int main(int argc, char **argv) { const Pvoid_t params = read_parameters(); test_param(params, "test1", "1,2,3"); test_param(params, "one two three", "dim\ndam\n"); test_param(params, "dummy", "value"); msg("All parameters ok!"); const char DVAL[] = "dkey"; p_entry *dkey = dxmalloc(sizeof(p_entry) + sizeof(DVAL)); dkey->len = sizeof(DVAL) - 1; memcpy(dkey->data, DVAL, dkey->len); int i = 0; p_entry *key = NULL; p_entry *val = NULL; while (read_kv(&key, &val)){ msg("Got key <%s> val <%s>", key->data, val->data); if (!key->len) key = dkey; write_num_prefix(3); write_kv(key, val); write_kv(key, val); write_kv(key, val); ++i; } msg("%d key-value pairs read ok", i); return 0; }
static void generate_system_info (GIOChannel *channel) { struct utsname u; memset (&u, 0, sizeof (u)); uname (&u); write_kv (channel, "uname.sysname", u.sysname); write_kv (channel, "uname.nodename", u.nodename); write_kv (channel, "uname.release", u.release); write_kv (channel, "uname.version", u.version); write_kv (channel, "uname.domainname", u.domainname); }
static int set_key_value(char *key, char *value) { char kpath[64] = {0}; sprintf(kpath, KV_STORE, key); if (access(KV_STORE_PATH, F_OK) == -1) { mkdir(KV_STORE_PATH, 0777); } return write_kv(kpath, value); }
static gint generate_support_data (const gchar *filename) { GIOChannel *channel; GError *error = NULL; if (g_str_equal (filename, "-")) { channel = g_io_channel_unix_new (0); } else if (!(channel = g_io_channel_new_file (filename, "w", &error))) { g_printerr ("%s\n", error->message); g_error_free (error); return EXIT_FAILURE; } g_io_channel_write_chars (channel, "[system]\n", -1, NULL, NULL); generate_date (channel); generate_system_info (channel); g_io_channel_write_chars (channel, "\n", -1, NULL, NULL); g_io_channel_write_chars (channel, "[perfkit]\n", -1, NULL, NULL); write_kv (channel, "lib.version", PK_VERSION_S); write_kv (channel, "agent.version", PKA_VERSION_S); g_io_channel_write_chars (channel, "\n", -1, NULL, NULL); generate_channels (channel); /* TODO */ if (!g_io_channel_flush (channel, &error)) { g_printerr ("%s\n", error->message); g_error_free (error); return EXIT_FAILURE; } g_io_channel_close (channel); return EXIT_SUCCESS; }
static void generate_date (GIOChannel *channel) { gchar datestr[64]; time_t t; struct tm tt; memset (&tt, 0, sizeof (tt)); memset (&datestr, 0, sizeof (datestr)); t = time (NULL); tt = *gmtime (&t); strftime (datestr, sizeof (datestr), "%Y-%m-%d %H:%M:%S", &tt); write_kv (channel, "date", datestr); }
//Write a node using enough items from the values list to create a node //with uncompressed size of at least mr_quota static couchstore_error_t flush_mr_partial(couchfile_modify_result *res, size_t mr_quota) { char *dst; int errcode = COUCHSTORE_SUCCESS; int itmcount = 0; char *nodebuf = NULL; sized_buf writebuf; char reducebuf[30]; size_t reducesize = 0; uint64_t subtreesize = 0; off_t diskpos; size_t disk_size; sized_buf final_key = {NULL, 0}; if (res->values_end == res->values || ! res->modified) { //Empty return COUCHSTORE_SUCCESS; } // nodebuf/writebuf is very short-lived and can be large, so use regular malloc heap for it: nodebuf = malloc(res->node_len + 1); if (!nodebuf) { return COUCHSTORE_ERROR_ALLOC_FAIL; } writebuf.buf = nodebuf; dst = nodebuf; *(dst++) = (char) res->node_type; nodelist *i = res->values->next; //We don't care that we've reached mr_quota if we haven't written out //at least two items and we're not writing a leaf node. while (i != NULL && (mr_quota > 0 || (itmcount < 2 && res->node_type == KP_NODE))) { dst = write_kv(dst, i->key, i->data); if (i->pointer) { subtreesize += i->pointer->subtreesize; } mr_quota -= i->key.size + i->data.size + 5; final_key = i->key; i = i->next; res->count--; itmcount++; } writebuf.size = dst - nodebuf; errcode = db_write_buf_compressed(res->rq->db, &writebuf, &diskpos, &disk_size); free(nodebuf); // here endeth the nodebuf. if (errcode != COUCHSTORE_SUCCESS) { return errcode; } if (res->node_type == KV_NODE && res->rq->reduce) { res->rq->reduce(reducebuf, &reducesize, res->values->next, itmcount); } if (res->node_type == KP_NODE && res->rq->rereduce) { res->rq->rereduce(reducebuf, &reducesize, res->values->next, itmcount); } node_pointer *ptr = (node_pointer *) arena_alloc(res->arena, sizeof(node_pointer) + final_key.size + reducesize); if (!ptr) { return COUCHSTORE_ERROR_ALLOC_FAIL; } ptr->key.buf = ((char *)ptr) + sizeof(node_pointer); ptr->reduce_value.buf = ((char *)ptr) + sizeof(node_pointer) + final_key.size; ptr->key.size = final_key.size; ptr->reduce_value.size = reducesize; memcpy(ptr->key.buf, final_key.buf, final_key.size); memcpy(ptr->reduce_value.buf, reducebuf, reducesize); ptr->subtreesize = subtreesize + disk_size; ptr->pointer = diskpos; nodelist *pel = encode_pointer(res->arena, ptr); if (!pel) { return COUCHSTORE_ERROR_ALLOC_FAIL; } res->pointers_end->next = pel; res->pointers_end = pel; res->node_len -= (writebuf.size - 1); res->values->next = i; if(i == NULL) { res->values_end = res->values; } return COUCHSTORE_SUCCESS; }
/* Write a node using enough items from the values list to create a node * with uncompressed size of at least mr_quota */ static couchstore_error_t flush_spatial_partial(couchfile_modify_result *res, size_t mr_quota) { char *dst; couchstore_error_t errcode = COUCHSTORE_SUCCESS; int itmcount = 0; char *nodebuf = NULL; sized_buf writebuf; char reducebuf[MAX_REDUCTION_SIZE]; size_t reducesize = 0; uint64_t subtreesize = 0; cs_off_t diskpos; size_t disk_size; nodelist *i, *pel; node_pointer *ptr; if (res->values_end == res->values || ! res->modified) { /* Empty */ return COUCHSTORE_SUCCESS; } /* nodebuf/writebuf is very short-lived and can be large, so use regular * malloc heap for it: */ nodebuf = (char *) cb_malloc(res->node_len + 1); if (nodebuf == NULL) { return COUCHSTORE_ERROR_ALLOC_FAIL; } writebuf.buf = nodebuf; dst = nodebuf; *(dst++) = (char) res->node_type; i = res->values->next; /* We don't care that we've reached mr_quota if we haven't written out * at least two items and we're not writing a leaf node. */ while (i != NULL && (mr_quota > 0 || (itmcount < 2 && res->node_type == KP_NODE))) { dst = (char *) write_kv(dst, i->key, i->data); if (i->pointer) { subtreesize += i->pointer->subtreesize; } mr_quota -= i->key.size + i->data.size + sizeof(raw_kv_length); i = i->next; res->count--; itmcount++; } writebuf.size = dst - nodebuf; errcode = (couchstore_error_t) db_write_buf_compressed( res->rq->file, &writebuf, &diskpos, &disk_size); cb_free(nodebuf); /* here endeth the nodebuf. */ if (errcode != COUCHSTORE_SUCCESS) { return errcode; } /* Store the enclosing MBB in the reducebuf */ if (res->node_type == KV_NODE && res->rq->reduce) { errcode = res->rq->reduce( reducebuf, &reducesize, res->values->next, itmcount, res->rq->user_reduce_ctx); if (errcode != COUCHSTORE_SUCCESS) { return errcode; } cb_assert(reducesize <= sizeof(reducebuf)); } if (res->node_type == KP_NODE && res->rq->rereduce) { errcode = res->rq->rereduce( reducebuf, &reducesize, res->values->next, itmcount, res->rq->user_reduce_ctx); if (errcode != COUCHSTORE_SUCCESS) { return errcode; } cb_assert(reducesize <= sizeof(reducebuf)); } /* `reducesize` one time for the key, one time for the actual reduce */ ptr = (node_pointer *) arena_alloc( res->arena, sizeof(node_pointer) + 2 * reducesize); if (ptr == NULL) { return COUCHSTORE_ERROR_ALLOC_FAIL; } ptr->key.buf = ((char *)ptr) + sizeof(node_pointer); ptr->reduce_value.buf = ((char *)ptr) + sizeof(node_pointer) + reducesize; ptr->key.size = reducesize; ptr->reduce_value.size = reducesize; /* Store the enclosing MBB that was calculate in the reduce function * as the key. The reduce also stores it as it is the "Original MBB" * used in the RR*-tree algorithm */ memcpy(ptr->key.buf, reducebuf, reducesize); memcpy(ptr->reduce_value.buf, reducebuf, reducesize); ptr->subtreesize = subtreesize + disk_size; ptr->pointer = diskpos; pel = encode_pointer(res->arena, ptr); if (pel == NULL) { return COUCHSTORE_ERROR_ALLOC_FAIL; } res->pointers_end->next = pel; res->pointers_end = pel; res->node_len -= (writebuf.size - 1); res->values->next = i; if(i == NULL) { res->values_end = res->values; } return COUCHSTORE_SUCCESS; }
static void generate_channels (GIOChannel *channel) { PkConnection *conn; PkChannels *channels; GList *list, *iter; GError *error = NULL; gchar *tmp, **tmpv; g_io_channel_write_chars (channel, "\n", -1, NULL, NULL); if (!(conn = pk_connection_new_for_uri ("dbus://"))) { g_io_channel_write_chars (channel, "## Could not access perfkit-agent!\n", -1, NULL, NULL); goto finish; } if (!pk_connection_connect (conn, &error)) { g_printerr ("Could not talk to perfkit-agent: %s\n", error->message); g_error_free (error); return; } channels = pk_connection_get_channels (conn); list = pk_channels_find_all (channels); for (iter = list; iter; iter = iter->next) { tmp = g_strdup_printf ("[channel-%d]\n", pk_channel_get_id (iter->data)); g_io_channel_write_chars (channel, tmp, -1, NULL, NULL); g_free (tmp); tmp = pk_channel_get_target (iter->data); write_kv (channel, "target", tmp); g_free (tmp); tmpv = pk_channel_get_args (iter->data); tmp = g_strjoinv (" ", tmpv); write_kv (channel, "args", tmp); g_free (tmp); g_strfreev (tmpv); tmp = pk_channel_get_dir (iter->data); write_kv (channel, "dir", tmp); g_free (tmp); tmpv = pk_channel_get_env (iter->data); tmp = g_strjoinv ("', '", tmpv); if (tmp && *tmp) tmp = g_strdup_printf ("'%s'", tmp); /* Leak */ write_kv (channel, "env", tmp); g_free (tmp); g_strfreev (tmpv); tmp = g_strdup_printf ("pid = %u\n", (guint)pk_channel_get_pid (iter->data)); g_io_channel_write_chars (channel, tmp, -1, NULL, NULL); g_free (tmp); tmp = g_strdup_printf ("state = %d\n", (gint)pk_channel_get_state (iter->data)); g_io_channel_write_chars (channel, tmp, -1, NULL, NULL); g_free (tmp); g_io_channel_write_chars (channel, "\n", -1, NULL, NULL); } g_list_foreach (list, (GFunc)g_object_unref, NULL); g_list_free (list); finish: g_io_channel_write_chars (channel, "\n", -1, NULL, NULL); }