#include #include #include #include #define FILENAME "./test_easy_fieldstat.json" struct thread_date { int thread_id; struct fieldstat_easy *fse; struct field *tag1; struct field *tag2; int counter_id; int hdr_id; }; void *thread_function(void *arg) { struct thread_date *td = (struct thread_date *)arg; int thread_id = td->thread_id; struct fieldstat_easy *fse = td->fse; int counter_id = td->counter_id; int hdr_id = td->hdr_id; while (1) { fieldstat_easy_counter_incrby(fse, thread_id, counter_id, td->tag1, 1, 1); fieldstat_easy_counter_incrby(fse, thread_id, counter_id, td->tag2, 1, 2); sleep(1); // fieldstat_easy_histogram_record(fse, thread_id, hdr_id, td->tag1, 1, rand() % 10000); } return NULL; } int main() { const int N_THREADS = 3; struct field global_tags[1]; struct field tmptag; tmptag.key = "app id"; tmptag.type = FIELD_VALUE_INTEGER; tmptag.value_longlong = 1; global_tags[0] = tmptag; struct field tag1 = {"direction", FIELD_VALUE_CSTRING, {.value_str = "incoming"}}; struct field tag2 = {"direction", FIELD_VALUE_CSTRING, {.value_str = "outgoing"}}; struct fieldstat_easy *fse = fieldstat_easy_new(N_THREADS, "n23", global_tags, 1); int counter_id = fieldstat_easy_register_counter(fse, "bytes"); int hdr_id = 0; // int hdr_id = fieldstat_easy_register_histogram( // fse, "delay", // 1, // min trackable value, no less than 1. The value less than this value will still be recorded, with an // // unpromising precision. // 10000, // max trackable value, the value larger than this value will be recorded as the max value. // 1); // precision, value less than 10 ^ precision will be recorded keeping exact precision. The larger recorded // // value is, the less precise of its count is. int output_interval_seconds = 1; fieldstat_easy_enable_auto_output(fse, FILENAME, output_interval_seconds); // FILENAME is the same as the one you set in python. pthread_t threads[N_THREADS]; struct thread_date thread_ids[N_THREADS]; for (int thread_id = 0; thread_id < N_THREADS; thread_id++) { thread_ids[thread_id] = (struct thread_date){thread_id, fse, &tag1, &tag2, counter_id, hdr_id}; pthread_create(&threads[thread_id], NULL, thread_function, &thread_ids[thread_id]); } sleep(1000); fieldstat_easy_free(fse); return 0; }