?? data.c
字號:
#include "data.h"database *init_database (){ int i; database *data = malloc (sizeof (database)); // cpu glibtop_cpu *cpu = malloc (sizeof (glibtop_cpu)); glibtop_get_cpu (cpu); cpustorage *cpustore = malloc (sizeof (cpustorage)); cpustore->olddata = cpu; // fill cpuusage (list) cpuusage *use = malloc (sizeof (cpuusage)); use->total = cpu->total; use->user = cpu->user; use->nice = cpu->nice; use->sys = cpu->sys; use->idle = cpu->idle; use->frequency = cpu->frequency; use->next = NULL; data->Lcpu = use; data->Scpu = cpustore; cpuusage *temp = use; i = 0; while (cpu->xcpu_total[i] != 0) { cpuusage *neu = malloc (sizeof (cpuusage)); neu->total = cpu->xcpu_total[i]; neu->user = cpu->xcpu_user[i]; neu->nice = cpu->xcpu_nice[i]; neu->sys = cpu->xcpu_sys[i]; neu->idle = cpu->xcpu_idle[i]; neu->next = NULL; temp->next = neu; temp = neu; i++; } // memoryusage glibtop_mem *mem = malloc (sizeof (glibtop_mem)); data->memoryusage = mem; // TODO: make this dynamic // fill netload (list) netstorage *seth0 = malloc (sizeof (netload)); netload *ueth0 = malloc (sizeof (netload)); glibtop_netload *geth0 = malloc (sizeof (glibtop_netload)); glibtop_get_netload (geth0, "eth0"); seth0->olddata = geth0; seth0->next = NULL; strcpy (ueth0->device, "eth0"); ueth0->next = NULL; data->Snetload = seth0; data->Lnetload = ueth0; // process list data->Lprocess = create_processlist (); find_top_processes (data->Lprocess, data->Tprocess, NUMBER_PROCESSES); // list of mounted filesystems glibtop_mountlist *mountlist = malloc (sizeof (glibtop_mountlist)); // get a list of all fs glibtop_mountentry *mountentry = glibtop_get_mountlist (mountlist, 0); csm_fsinfo *previous; for (i = 0; i < mountlist->number; i++) { csm_fsinfo *neu = malloc (sizeof (csm_fsinfo)); strcpy (neu->devname, mountentry[i].devname); strcpy (neu->mountdir, mountentry[i].mountdir); strcpy (neu->type, mountentry[i].type); //neu->mountentry = &mountentry[i]; glibtop_fsusage *usage = malloc (sizeof (glibtop_fsusage)); // get usage glibtop_get_fsusage (usage, mountentry[i].mountdir); // don't "use" filesystems with zero blocks or tmpfs if (usage->blocks == 0 || !strcmp (neu->type, "tmpfs")) continue; neu->fsusage = usage; // handle first node if (data->fsinfo == NULL) { data->fsinfo = neu; neu->prev = NULL; neu->next = NULL; previous = neu; continue; } neu->prev = previous; previous->next = neu; previous = neu; neu->next = NULL; } g_free (mountentry); free (mountlist); // XXX: hack! (prevents division by zero in some cases) // Bugs are fixed, this is for history reasons only;-) usleep (100000); return data;}voidfree_database (database * data){ g_free (data->Scpu->olddata); g_free (data->Scpu); g_free (data->memoryusage); //free Lcpu cpuusage *cputemp = data->Lcpu; cpuusage *cpudelinquent; while (cputemp != NULL) { cpudelinquent = cputemp; cputemp = cputemp->next; g_free (cpudelinquent); } g_free (cputemp); //free Lnetload netload *nettemp = data->Lnetload; netload *netdelinquent; while (nettemp != NULL) { netdelinquent = nettemp; nettemp = nettemp->next; g_free (netdelinquent); } g_free (nettemp); //free Snetload netstorage *snettemp = data->Snetload; netstorage *snetdelinquent; while (snettemp != NULL) { snetdelinquent = snettemp; snettemp = snettemp->next; g_free (snetdelinquent->olddata); g_free (snetdelinquent); } g_free (snettemp); // free processes process *ptemp = data->Lprocess; process *prev; while (ptemp != NULL) { prev = ptemp; ptemp = ptemp->next; free (prev); } // TODO: free filesystems free (ptemp); g_free (data);}/** * Methods which fill the data storage with the actual content */voidcalc_cpu (database * data){ glibtop_cpu *cpu = g_malloc (sizeof (glibtop_cpu)); glibtop_get_cpu (cpu); cpustorage *storage = data->Scpu; data->Lcpu->total = (cpu->total - storage->olddata->total); if (data->Lcpu->total != 0) { // if it is zero, no time has passed // since last invocation (and we would have division by zero) data->Lcpu->user = (cpu->user - storage->olddata->user) / data->Lcpu->total; data->Lcpu->nice = (cpu->nice - storage->olddata->nice) / data->Lcpu->total; data->Lcpu->sys = (cpu->sys - storage->olddata->sys) / data->Lcpu->total; data->Lcpu->idle = (cpu->idle - storage->olddata->idle) / data->Lcpu->total; data->Lcpu->frequency = cpu->frequency; // this is for smp systems, // calculate all other cpus cpuusage *temp = data->Lcpu->next; int i = 0; while (temp != NULL) { temp->total = (cpu->xcpu_total[i] - storage->olddata->xcpu_total[i]); temp->user = (cpu->xcpu_user[i] - storage->olddata->xcpu_user[i]) / temp->total; temp->nice = (cpu->xcpu_nice[i] - storage->olddata->xcpu_nice[i]) / temp->total; temp->sys = (cpu->xcpu_sys[i] - storage->olddata->xcpu_sys[i]) / temp->total; temp->idle = (cpu->xcpu_idle[i] - storage->olddata->xcpu_idle[i]) / temp->total; temp = temp->next; i++; } } free (storage->olddata); storage->olddata = cpu;}voidcalc_net (database * data){ netload *current_netload = data->Lnetload; // first netdevice netstorage *storage = data->Snetload; // walk list of interfaces while ((current_netload != NULL) && (storage != NULL)) { glibtop_netload *new = malloc (sizeof (glibtop_netload)); // get current info on device glibtop_get_netload (new, current_netload->device); glibtop_netload *old = storage->olddata; // calc netload current_netload->bytes_in = new->bytes_in - old->bytes_in; current_netload->bytes_out = new->bytes_out - old->bytes_out; current_netload->packets_in = new->packets_in - old->packets_in; current_netload->packets_out = new->packets_out - old->packets_out; current_netload->errors_in = new->errors_in - old->errors_in; current_netload->errors_out = new->errors_out - old->errors_out; current_netload->collisions = new->collisions - old->collisions; // free memory free (storage->olddata); storage->olddata = new; // goto next device current_netload = current_netload->next; storage = storage->next; }}voidcalc_mem (database * data){ glibtop_get_mem (data->memoryusage);}voidclean_csm_fsinfo (database * data){ csm_fsinfo *temp = data->fsinfo; while (temp != NULL) { // this fs is no longer mounted if (temp->touched == 0) { csm_fsinfo *save = temp->next; if (temp->next != NULL) temp->next->prev = temp->prev; if (temp->prev != NULL) temp->prev->next = temp->next; // free temp free (temp->fsusage); free (temp); temp = save; continue; } temp = temp->next; }}voidclean_csm_fsinfo_touched (database * data){ csm_fsinfo *temp = data->fsinfo; while (temp != NULL) { temp->touched = 0; temp = temp->next; }}voidupdate_csm_fsinfo (database * data){ glibtop_mountlist *mountlist = malloc (sizeof (glibtop_mountlist)); glibtop_mountentry *entries = glibtop_get_mountlist (mountlist, 0); int i, flag; for (i = 0; i < mountlist->number; i++) { flag = 0; csm_fsinfo *temp = data->fsinfo; while (temp != NULL) { // skip already found filesystems if (temp->touched == 1) { temp = temp->next; continue; } // is this mounted filesystem in our list? if (!strcmp (entries[i].mountdir, temp->mountdir)) { temp->touched = 1; flag = 1; break; } temp = temp->next; } // filesystem is not yet in mountlist if (flag == 0) { // make new csm_fsinfo csm_fsinfo *neu = malloc (sizeof (csm_fsinfo)); strcpy (neu->devname, entries[i].devname); strcpy (neu->mountdir, entries[i].mountdir); strcpy (neu->type, entries[i].type); neu->touched = 1; glibtop_fsusage *usage = malloc (sizeof (glibtop_fsusage)); glibtop_get_fsusage (usage, entries[i].mountdir); neu->fsusage = usage; // don't use tmpfs and filesystems with zero blockcount if (usage->blocks == 0 || !strcmp (neu->type, "tmpfs")) { free (neu->fsusage); free (neu); continue; } neu->next = NULL; // goto last listitem temp = data->fsinfo; while (temp->next != NULL) { temp = temp->next; } temp->next = neu; neu->prev = temp; } // fi } // rof g_free (mountlist); g_free (entries); // clean list clean_csm_fsinfo (data); // reset csm_fsinfo->touched clean_csm_fsinfo_touched (data);}voidcalc_disk (database * data){ // update list of mounted filesystems update_csm_fsinfo (data); csm_fsinfo *temp = data->fsinfo; while (temp != NULL) { // update fsusage glibtop_get_fsusage (temp->fsusage, temp->mountdir); temp = temp->next; }}voidcalc_top_processes (database * data){ update_processlist (data->Lprocess); find_top_processes (data->Lprocess, data->Tprocess, NUMBER_PROCESSES);}/** * Utility functions */voidinit_csm (){ glibtop_init ();}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -