Ejemplo n.º 1
0
static void console_int(uint32_t port, void *data)
{
	XENCONS_RING_IDX cons, prod;
	cons = console.intf->in_cons;
	prod = console.intf->in_prod;
	rmb();

	if (prod == cons)
		return;

	uint32_t in_size = prod - cons;
	uint8_t buf[in_size];
	uint8_t *ptr = buf;

	ssa(SYS_STATS_IO_INPUT, in_size);

	while (prod > cons)
	{
		int idx = MASK_XENCONS_IDX(cons++, console.intf->in);
#ifdef DEBUG_CONSOLE
		if (debug_key(console.intf->in[idx]))
		{
			in_size--;
			continue;
		}
#endif
		*ptr++ = console.intf->in[idx];
	}

	console.intf->in_cons = prod;
	wmb();

	if (console.attached)
		outlet_new_data(console.attached, buf, in_size);
}
Ejemplo n.º 2
0
int memcache_get (struct connection *c, const char *key, int len) {
  char *ptr;
  //char timezone[32];
  long long cnt_id;
  if (verbosity >= 4) {
    fprintf (stderr, "memcache_get (key = \"");
    debug_key (key, len);
    fprintf (stderr, "\")\n");
  }
  int dog_len = get_at_prefix_length (key, len);
  key += dog_len;
  len -= dog_len;

  Q_raw = 0;
  if (len > 0 && *key == '%') {
    dog_len ++;
    key ++;
    len --;
    Q_raw = 1;
  }


  if (len > 5 && !strncmp (key, "views", 5)) {
    int ver;
    if (!parse_countid_with_version (key + 5, &cnt_id, &ver)) {
      debug_error ("get", "couldn't parse count_id&version", key, len);
      return not_found (c);
    }
    //int res = get_counter_views(cnt_d, ver); TODO!!!
    int res = (ver == -1) ? get_counter_views (cnt_id) : get_counter_views_given_version (cnt_id,ver);
    if (res == -2) {
      return memcache_wait (c);
    }
    if (res >= 0) {
      //int return_one_key (struct connection *c, const char *key, char *val, int vlen) {
      if (!Q_raw) {
        return_one_key (c, key - dog_len, stats_buff, sprintf (stats_buff, "%d", res));
      } else {
        *(int *)stats_buff = res;
        return_one_key (c, key - dog_len, stats_buff, sizeof (int));
      }
    }
    return 0;
  }

  if (len > 8 && !strncmp (key, "visitors", 8)) {
    int ver;
    if (!parse_countid_with_version (key + 8, &cnt_id, &ver)) {
      debug_error ("get","couldn't parse count_id&version",key, len);
      return not_found(c);
    }
    //int res = get_counter_visitors(cnt_id, ver); TODO !!!
    int res = (ver == -1) ? get_counter_visitors (cnt_id) : get_counter_visitors_given_version (cnt_id, ver);
    if (res == -2) {
      return memcache_wait (c);
    }
    if (res >= 0) {
      //int return_one_key (struct connection *c, const char *key, char *val, int vlen) {
      if (!Q_raw) {
        return_one_key (c, key - dog_len, stats_buff, sprintf (stats_buff, "%d", res));
      } else {
        *(int *)stats_buff = res;
        return_one_key (c, key - dog_len, stats_buff, sizeof (int));
      }
    }
    return 0;
  }

  if (len > 14 && !strncmp (key, "enable_counter", 14)) {
    cnt_id = strtoll (key + 14, &ptr, 10);
    if (ptr > key + 14 && !*ptr) {
      int res = enable_counter (cnt_id, 0);
      if (res >= 0) {
      if (!Q_raw) {
        return_one_key (c, key - dog_len, stats_buff, sprintf (stats_buff, "%d", res));
      } else {
        *(int *)stats_buff = res;
        return_one_key (c, key - dog_len, stats_buff, sizeof (int));
      }
      }
      return 0;
    }
  }

  if (len > 15 && !strncmp (key, "disable_counter", 15)) {
    cnt_id = strtoll (key + 15, &ptr, 10);
    if (ptr > key + 15 && !*ptr) {
      int res = disable_counter (cnt_id, 0);
      if (!Q_raw) {
        return_one_key (c, key - dog_len, stats_buff, sprintf (stats_buff, "%d", res));
      } else {
        *(int *)stats_buff = res;
        return_one_key (c, key - dog_len, stats_buff, sizeof (int));
      }
      return 0;
    }
  }

  if (len > 12 && !strncmp(key, "set_timezone", 12)) {
    int tz = 0;
    //if (2 == sscanf(key+12,"%d#%31s",&cnt_id,timezone)) {
    if (2 == sscanf(key + 12,"%lld#%d", &cnt_id, &tz)) {
      tz = tz + 12 + 4;
      if (tz < 0) {
        return 0;
      }
      int res = set_timezone (cnt_id, tz, 0);
      if (!Q_raw) {
        return_one_key (c, key - dog_len, stats_buff, sprintf (stats_buff, "%d", res));
      } else {
        *(int *)stats_buff = res;
        return_one_key (c, key - dog_len, stats_buff, sizeof (int));
      }
      return 0;
    }
  }

  if (len > 8 && !strncmp(key, "timezone", 8)) {
    if (1 == sscanf(key + 8, "%lld", &cnt_id)) {
      int res = get_timezone (cnt_id);
      if (!Q_raw) {
        return_one_key (c, key - dog_len, stats_buff, sprintf (stats_buff, "%d", res));
      } else {
        *(int *)stats_buff = res;
        return_one_key (c, key - dog_len, stats_buff, sizeof (int));
      }
      return 0;
    }
  }


  if (len > 7 && !strncmp (key, "counter", 7)) {
    int ver = 0;
    if (sscanf (key, "counter%lld@%d", &cnt_id, &ver) >= 1) {
      int to_serialize = key[strlen(key) - 1] != '?';
      if (verbosity >= 4) {
        fprintf(stderr, "cnt_id = %lld, ver = %d\n", cnt_id, ver);
      }
      int res = get_counter_serialized (stats_buff, cnt_id, ver);
      if (res == -2) {
        return memcache_wait (c);
      }
      if (res > 0) {
        if (to_serialize && !Q_raw) {
          write_out (&c->Out, stats_buff+res, sprintf (stats_buff+res, "VALUE %s 1 %d\r\n", key - dog_len, res));
        } else {
          write_out (&c->Out, stats_buff+res, sprintf (stats_buff+res, "VALUE %s 0 %d\r\n", key - dog_len, res));
        }
        write_out (&c->Out, stats_buff, res);
        write_out (&c->Out, "\r\n", 2);
      }
      return 0;
    }
  }

  if (len > 16 && !strncmp (key, "monthly_visitors", 16)) {
    cnt_id = 0;
    if (sscanf (key, "monthly_visitors%lld", &cnt_id) >= 1) {
      int res = get_monthly_visitors_serialized (stats_buff, cnt_id);
      if (res == -2) {
        return memcache_wait (c);
      }
      if (res >= 0) {
        return_one_key (c, key - dog_len, stats_buff, res);
      }
      return 0;
    }
  }

  if (len > 13 && !strncmp (key, "monthly_views", 13)) {
    cnt_id = 0;
    if (sscanf (key, "monthly_views%lld", &cnt_id) >= 1) {
      int res = get_monthly_views_serialized (stats_buff, cnt_id);
      if (res == -2) {
        return memcache_wait (c);
      }
      if (res >= 0) {
        return_one_key (c, key - dog_len, stats_buff, res);
      }
      return 0;
    }
  }

  if (len > 12 && !strncmp (key, "counters_sum", 12)) {
    int start_id = 0, finish_id = 0, id = 0;
    int to_serialize = key[strlen(key) - 1] != '?';
    if (sscanf (key, "counters_sum%d_%d_%d", &id, &start_id, &finish_id)) {
      struct counter *C = get_counters_sum (id, start_id, finish_id);
      if (C == (void *)-2l) { 
        return memcache_wait (c);
      }
      if (C) {
        int res = counter_serialize (C, stats_buff);
        assert (res >= 0);
        if (to_serialize && !Q_raw) {
          write_out (&c->Out, stats_buff+res, sprintf (stats_buff+res, "VALUE %s 1 %d\r\n", key - dog_len, res));
        } else {
          write_out (&c->Out, stats_buff+res, sprintf (stats_buff+res, "VALUE %s 0 %d\r\n", key - dog_len, res));
        }
        write_out (&c->Out, stats_buff, res);
        write_out (&c->Out, "\r\n", 2);
      }
    }
  }
  /*
  if (len > 7 && !strncmp (key, "counter", 7)) {
    int cnt_id, subcnt_id = -1, ver = 0;
    if (sscanf (key, "counter%d:%d@%d", &cnt_id, &subcnt_id, &ver) >= 2) {
      //int res = get_counter_serialized (stats_buff, cnt_id, subcnt_id, ver); TODO !!!
      int res = get_counter_serialized (stats_buff, cnt_id, ver);
      if (res > 0) {
        write_out (&c->Out, stats_buff+res, sprintf (stats_buff+res, "VALUE %s 1 %d\r\n", key, res));
        write_out (&c->Out, stats_buff, res);
        write_out (&c->Out, "\r\n", 2);
      }
      return 0;
    }
    if (sscanf (key, "counter%d@%d", &cnt_id, &ver) == 2) {
      //int res = get_counter_serialized (stats_buff, cnt_id, -1, ver); TODO !!!
      int res = get_counter_serialized (stats_buff, cnt_id, ver);
      if (res > 0) {
        write_out (&c->Out, stats_buff+res, sprintf (stats_buff+res, "VALUE %s 1 %d\r\n", key, res));
        write_out (&c->Out, stats_buff, res);
        write_out (&c->Out, "\r\n", 2);
      }
      return 0;
    }
  }
  */

  if (len > 8 && !strncmp (key, "versions", 8)) {
    cnt_id = strtoll (key + 8, &ptr, 10);
    if (ptr > key+8 && !*ptr) {
      int res = get_counter_versions (stats_buff, cnt_id);
      if (res == -2) {
        return memcache_wait (c);
      }
      if (res > 0) {
        write_out (&c->Out, stats_buff + res, sprintf (stats_buff + res, "VALUE %s 0 %d\r\n", key - dog_len, res));
        write_out (&c->Out, stats_buff, res);
        write_out (&c->Out, "\r\n", 2);
      }
      return 0;
    }
  }

  if (len >= 16 && !strncmp (key, "free_block_stats", 16)) {
    return_one_key_list (c, key - dog_len, len + dog_len, MAX_RECORD_WORDS, 0, FreeCnt, MAX_RECORD_WORDS);
    return 0;
  }

  if (len >= 16 && !strncmp (key, "used_block_stats", 16)) {
    return_one_key_list (c, key - dog_len, len + dog_len, MAX_RECORD_WORDS, 0, UsedCnt, MAX_RECORD_WORDS);
    return 0;
  }

  if (len >= 16 && !strncmp (key, "allocation_stats", 16)) {
    return_one_key_list (c, key - dog_len, len + dog_len, MAX_RECORD_WORDS, 0, NewAllocations[0], MAX_RECORD_WORDS * 4);
    return 0;
  }

  if (len >= 17 && !strncmp (key, "split_block_stats", 17)) {
    return_one_key_list (c, key - dog_len, len + dog_len, MAX_RECORD_WORDS, 0, SplitBlocks, MAX_RECORD_WORDS);
    return 0;
  }

  if (len >= 5 && !strncmp (key, "stats", 5)) {
    return_one_key (c, key - dog_len, stats_buff, stats_prepare_stats (c));
    return 0;
  }

  return 0;
}
Ejemplo n.º 3
0
int memcache_incr (struct connection *c, int op, const char *key, int len, long long arg) {
  if (verbosity >= 4) {
    fprintf (stderr, "memcache_incr (op = %d, key = \"", op);
    debug_key (key, len);
    fprintf (stderr, "\")\n");
  }

  const int dog_len = get_at_prefix_length (key, len);
  key += dog_len;
  len -= dog_len;

  if (len >= 7 && !memcmp (key, "counter", 7)) {
    long long cnt_id, tmp;
    int  subcnt_id=-1,  uid, city = 0, res;
    char sex = 0, age = 0, status = 0, polit = 0, section = 0, region[4], country[4], source = 0, *p;
    char optional_params_is_given = 1;
    int version = -1;
    memset (region, 0, sizeof (region));
    memset (country, 0, sizeof (country));
    errno = 0; tmp = strtoll (key + 7, &p, 10);
    if (errno) {
      debug_error ("incr", "fail to parse counter_id", key, len);
      return not_found (c);
    }
    cnt_id = tmp;
    if (*p == '@') {
      errno = 0; tmp = strtol(p + 1, &p, 10);
      if (errno || tmp < 0) {
        debug_error ("incr", "fail to parse version",  key, len);
        return not_found (c);
      }
      if (verbosity >= 3) {
        fprintf (stderr, "version = %d\n", version);
      }
      version = (int) tmp;
    }
    if (*p == ':') {
      errno = 0; tmp = strtol(p+1, &p, 10);
      if (errno) {
        debug_error ("incr", "fail to parse subcnt_id",  key, len);
        return not_found (c);
      }
      subcnt_id = (int) tmp;
    }
    if (*p != '#') {
      debug_error ("incr", "missed uid (expected '#')", key, len);
      return not_found (c);
    }
    errno = 0; tmp = strtol (p+1, &p, 10);
    if (errno) {
      debug_error ("incr", "fail to parse uid", key, len);
      return not_found (c);
    }
    uid = (int) tmp;
    if (*p && *p != '#') {
      debug_error ("incr", "expected '#' after uid", key, len);
      return not_found (c);
    }
    if (verbosity >= 4) {
      fprintf (stderr, "incr (cnt_id = %lld, subcnt_id = %d, uid = %d)\n", cnt_id, subcnt_id, uid);
    }
    if (!(*p)) optional_params_is_given = 0;
    if (*p) p++;
    if (*p) sex = *p++;
    if (*p) age = *p++;
    if (*p) status = *p++;
    if (*p) polit = *p++;
    if (*p) section = *p++;
    if (*p == ';') {
      sscanf (p+1,"%d;%3[^;];%3[^;];%c", &city, region, country, &source);
    }
    if (sex > '0' && sex <= '2') { sex -= '0'; } else { sex = 0; }
    if (age > '0' && age <= '8') { age -= '0'; } else { age = 0; }
    if (status > '0' && status <= '8') { status -= '0'; } else { status = 0; }
    if (polit > '0' && polit <= '8') { polit -= '0'; } else { polit = 0; }
    if (section >= 'A' && section <= 'P') { section -= 'A' - 1; } else { section = 0; }
    if (source >= 'A' && source <= 'P') { source -= 'A' - 1; } else { source = 0; }
    if (verbosity >= 4) {
      fprintf (stderr, "optional_params_is_given = %d\n", optional_params_is_given);
      fprintf (stderr, "sex = %d, age = %d, status = %d, polit = %d, section = %d, city = %d, region = %d, country = %d, source = %d\n",
             sex, age, status, polit, section, city, char3_to_int(region), char3_to_int(country),(int) source);
    }
    //int counter_incr_ext (int counter_id, int user_id, int replaying, int op, int subcnt, int sex, int age, int m_status, int polit_views, int section, int city, int country, int geoip_country, int source);
    incr_version = version;
    incr_counter_id = cnt_id;
    incr_version_read = 0;
    if ((version >= 0 && !custom_version_names) || (version < 0 && custom_version_names)) {
      debug_error ("incr", "fail due to version",  key, len);
      return not_found (c);
    }
    res = (optional_params_is_given && subcnt_id == -1) ?
           counter_incr_ext (cnt_id, uid, 0, op, subcnt_id, sex, age, status, polit, section, city, char3_to_int(region), char3_to_int(country), source) :
           counter_incr (cnt_id, uid, 0, op, subcnt_id);
    //int counter_incr (int counter_id, int user_id, int replaying, int op, int subcnt);
    if (res < 0) return not_found (c);
    write_out (&c->Out, stats_buff, sprintf (stats_buff, "%d\r\n", res));
    return 0;
  }
  return not_found (c);
}
Ejemplo n.º 4
0
void debug_error (const char *szAction, const char *szMsg, const char *key, int len) {
  if (!verbosity) { return; }
  fprintf (stderr, "Bad %s (key=\"", szAction);
  debug_key (key, len);
  fprintf (stderr, "\"); %s\n", szMsg);
}