int operator()() { if (_fp == NULL) { return INVALID_VALUE; } // only calculate each 1s if(lastValue.CheckUpdate(1*1000)) { if (!read_fields()) { return INVALID_VALUE; } total_tick_old = total_tick; idle_old = idle; if (!read_fields()) { return INVALID_VALUE; } total_tick = std::accumulate(std::begin(fields), std::end(fields), (uint64_t)0); idle = fields[3]; del_total_tick = total_tick - total_tick_old; del_idle = idle - idle_old; } return std::lrint(((del_total_tick - del_idle) / (double) del_total_tick) * 100); }
int main (void) { FILE *fp; unsigned long long int fields[10], total_tick, total_tick_old, idle, idle_old, del_total_tick, del_idle; int update_cycle = 0, i, flag = 1; double percent_usage; fp = fopen ("/proc/stat", "r"); if (fp == NULL) { perror ("Error"); } if (!read_fields (fp, fields)) { return 0; } for (i=0, total_tick = 0; i<10; i++) { total_tick += fields[i]; } idle = fields[3]; /* idle ticks index */ while (flag) { sleep (1); total_tick_old = total_tick; idle_old = idle; fseek (fp, 0, SEEK_SET); fflush (fp); if (!read_fields (fp, fields)) { return 0; } for (i=0, total_tick = 0; i<10; i++) { total_tick += fields[i]; } idle = fields[3]; del_total_tick = total_tick - total_tick_old; del_idle = idle - idle_old; percent_usage = ((del_total_tick - del_idle) / (double) del_total_tick) * 100; /* 3 is index of idle time */ printf ("%3.2lf\n", percent_usage); update_cycle++; } fclose (fp); /* Ctrl + C quit, therefore this will not be reached. We rely on the kernel to close this file */ return 0; }
void ReadGame(const char *filename) { FILE *f; int i; gi.FreeTags(TAG_GAME); f = fopen(filename, "rb"); if (!f) gi.error("Couldn't open %s", filename); i = read_int(f); if (i != SAVE_MAGIC1) { fclose(f); gi.error("Not a save game"); } i = read_int(f); if (i != SAVE_VERSION) { fclose(f); gi.error("Savegame from an older version"); } read_fields(f, gamefields, &game); // should agree with server's version if (game.maxclients != (int)maxclients->value) { fclose(f); gi.error("Savegame has bad maxclients"); } if (game.maxentities <= game.maxclients || game.maxentities > MAX_EDICTS) { fclose(f); gi.error("Savegame has bad maxentities"); } g_edicts = gi.TagMalloc(game.maxentities * sizeof(g_edicts[0]), TAG_GAME); globals.edicts = g_edicts; globals.max_edicts = game.maxentities; game.clients = gi.TagMalloc(game.maxclients * sizeof(game.clients[0]), TAG_GAME); for (i = 0; i < game.maxclients; i++) { read_fields(f, clientfields, &game.clients[i]); } fclose(f); }
int OptionsDialog::exec() { read_fields(); bool accepted = (QDialog::exec() == QDialog::Accepted); if (accepted) { write_fields(); } return accepted; }
GetCpuLoad_Singleton() { _fp = fopen("/proc/stat", "r"); if (_fp && read_fields()) { total_tick = std::accumulate(std::begin(fields), std::end(fields), (uint64_t)0); idle = fields[3]; /* idle ticks index */ } lastValue.Update(); }
static int read_row(drizzle_con_st *con, drizzle_result_st *result) { int status = 1; uint64_t read_row = 0; drizzle_return_t ret; PyObject *field = NULL; while(status){ read_row = drizzle_row_read(result, &ret); status = io_wait(con, ret); if (status == -1){ goto error; } } if (read_row == 0) { return 0; } field = read_fields(con, result); return 1; error: return -1; }
/* ================= ReadLevel SpawnEntities will allready have been called on the level the same way it was when the level was saved. That is necessary to get the baselines set up identically. The server will have cleared all of the world links before calling ReadLevel. No clients are connected yet. ================= */ void ReadLevel(const char *filename) { int entnum; FILE *f; int i; edict_t *ent; // free any dynamic memory allocated by loading the level // base state gi.FreeTags(TAG_LEVEL); f = fopen(filename, "rb"); if (!f) gi.error("Couldn't open %s", filename); // wipe all the entities memset(g_edicts, 0, game.maxentities * sizeof(g_edicts[0])); globals.num_edicts = maxclients->value + 1; i = read_int(f); if (i != SAVE_MAGIC2) { fclose(f); gi.error("Not a save game"); } i = read_int(f); if (i != SAVE_VERSION) { fclose(f); gi.error("Savegame from an older version"); } // load the level locals read_fields(f, levelfields, &level); // load all the entities while (1) { entnum = read_int(f); if (entnum == -1) break; if (entnum < 0 || entnum >= game.maxentities) { gi.error("%s: bad entity number", __func__); } if (entnum >= globals.num_edicts) globals.num_edicts = entnum + 1; ent = &g_edicts[entnum]; read_fields(f, entityfields, ent); ent->inuse = qtrue; ent->s.number = entnum; // let the server rebuild world links for this ent memset(&ent->area, 0, sizeof(ent->area)); gi.linkentity(ent); } fclose(f); // mark all clients as unconnected for (i = 0 ; i < maxclients->value ; i++) { ent = &g_edicts[i + 1]; ent->client = game.clients + i; ent->client->pers.connected = qfalse; } // do any load time things at this point for (i = 0 ; i < globals.num_edicts ; i++) { ent = &g_edicts[i]; if (!ent->inuse) continue; // fire any cross-level triggers if (ent->classname) if (strcmp(ent->classname, "target_crosslevel_target") == 0) ent->nextthink = level.time + ent->delay; } }
void read(ByteFile& file) { offset = file.position(); file.read_bytes(id).unwrap_ok(); file.read_bytes(size).unwrap_ok(); read_fields(file); }
void get_cpu_use(int *cpu, int len) { int count; #ifdef __APPLE__ // just so that I can test it in xcode on my mac... for (count = 0; count < len; count++) { cpu[count] = rand() % 100; } sleep(1); return; #else unsigned long long int fields[10], total_tick[len], total_tick_old[len], idle[len], idle_old[len], del_total_tick[len], del_idle[len]; int i, cpus = 0; double percent_usage; FILE *fp = fopen ("/proc/stat", "r"); if (fp == NULL) { return; } while (read_fields (fp, fields) != -1) { for (i=0, total_tick[cpus] = 0; i<10; i++) { total_tick[cpus] += fields[i]; } idle[cpus] = fields[3]; /* idle ticks index */ cpus++; } sleep (1); fseek (fp, 0, SEEK_SET); fflush (fp); for (count = 0; count < len; count++) { total_tick_old[count] = total_tick[count]; idle_old[count] = idle[count]; if (!read_fields (fp, fields)) { fclose (fp); return; } for (i=0, total_tick[count] = 0; i<10; i++) { total_tick[count] += fields[i]; } idle[count] = fields[3]; del_total_tick[count] = total_tick[count] - total_tick_old[count]; del_idle[count] = idle[count] - idle_old[count]; percent_usage = ((del_total_tick[count] - del_idle[count]) / (double) del_total_tick[count]) * 100; cpu[count] = (int)percent_usage; } fclose (fp); #endif }
int main (void) { FILE *fp; unsigned long long int fields[10], total_tick, total_tick_old, idle, idle_old, del_total_tick, del_idle; int update_cycle = 0, i, flag = 1; double percent_usage; CURL *curl; curl_global_init(CURL_GLOBAL_ALL); fp = fopen ("/proc/stat", "r"); if (fp == NULL) { perror ("Error"); } if (!read_fields (fp, fields)) { return 0; } for (i=0, total_tick = 0; i<10; i++) { total_tick += fields[i]; } idle = fields[3]; /* idle ticks index */ while (flag) { sleep (1); total_tick_old = total_tick; idle_old = idle; fseek (fp, 0, SEEK_SET); fflush (fp); if (!read_fields (fp, fields)) { return 0; } for (i=0, total_tick = 0; i<10; i++) { total_tick += fields[i]; } idle = fields[3]; del_total_tick = total_tick - total_tick_old; del_idle = idle - idle_old; percent_usage = ((del_total_tick - del_idle) / (double) del_total_tick) * 100; /* 3 is index of idle time */ char postthis[200]; curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_URL, "http://143.233.227.108:8086/write?db=flows"); curl_easy_setopt(curl, CURLOPT_POST, 1); sprintf(postthis, "cpu_usage,name=all value=%3.2lf%%\n",percent_usage); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis)); curl_easy_perform(curl); curl_easy_cleanup(curl); printf ("Total CPU Usage: %3.2lf%%\n", percent_usage); update_cycle++; } fclose (fp); /* Ctrl + C quit, therefore this will not be reached. We rely on the kernel to close this file */ return 0; }