Add sysinfo example

Retrieve cpu model via hashtable & number of logical cpu via exposed variable

Signed off: Dr. James Dominic P. Guana <jdpguana@histark.com>
This commit is contained in:
James Dominic P. Guana
2021-08-21 16:17:13 +00:00
committed by Robert Roth
parent e89475c0b2
commit f92f6ea029
3 changed files with 3 additions and 23 deletions

View File

@@ -32,13 +32,14 @@
int int
main (int argc, char *argv []) main (int argc, char *argv [])
{ {
char *model;
const glibtop_sysinfo * sysinfo; const glibtop_sysinfo * sysinfo;
glibtop_init(); glibtop_init();
sysinfo = glibtop_get_sysinfo (); sysinfo = glibtop_get_sysinfo ();
model = g_hash_table_lookup (sysinfo->cpuinfo [1].values, "model name");
printf ("\nProcessor Model: %s\n", sysinfo->model); printf ("\nProcessor Model: %s\n", g_strdup (model));
printf ("Number of Cores: %d\n\n", sysinfo->ncpu); printf ("Number of Cores: %d\n\n", sysinfo->ncpu);
glibtop_close (); glibtop_close ();

View File

@@ -51,7 +51,6 @@ struct _glibtop_sysinfo
guint64 flags; guint64 flags;
guint64 ncpu; guint64 ncpu;
glibtop_entry cpuinfo [GLIBTOP_NCPU]; glibtop_entry cpuinfo [GLIBTOP_NCPU];
gchar *model;
}; };
#define glibtop_get_sysinfo_r glibtop_get_sysinfo_s #define glibtop_get_sysinfo_r glibtop_get_sysinfo_s

View File

@@ -37,7 +37,6 @@ static void
init_sysinfo (glibtop *server) init_sysinfo (glibtop *server)
{ {
char* buffer; char* buffer;
gchar *buf, *line;
gchar ** processors; gchar ** processors;
if(G_LIKELY(sysinfo.flags)) return; if(G_LIKELY(sysinfo.flags)) return;
@@ -49,25 +48,6 @@ init_sysinfo (glibtop *server)
/* cpuinfo records are seperated by a blank line */ /* cpuinfo records are seperated by a blank line */
processors = g_strsplit(buffer, "\n\n", 0); processors = g_strsplit(buffer, "\n\n", 0);
line = strtok(processors[0], "\n");
while(line != NULL)
{
if (strstr(line, "model name") != NULL)
{
buf = strchr(line, ':');
if (buf != NULL)
{
/* we want to look at what's _after_ the ':' */
buf+=2;
sysinfo.model = g_strdup (buf);
}
break;
}
line = strtok(NULL, "\n");
}
g_free(buffer); g_free(buffer);
sysinfo.ncpu = 0; sysinfo.ncpu = 0;