OK, here's that sysmp code I wrote some time ago. It's nothing fancy, just reads the data.
If I remember correctly, the loads are in CPU time (ie. they tell you how much time in total has the CPU spent in user, kernel, idle, ... state), but don't take my word for it - you should probably check some manpages first
.
Code:
Select all
#include <stdio.h>
#include <string.h> /* for memset(), strerror() */
#include <unistd.h> /* for exit(), getpagesize() */
#include <sys/types.h>
#include <sys/sysmp.h>
#include <sys/sysinfo.h>
#include <errno.h>
/* Convert pages to kb */
#define p2kb(pages) ((((uint64_t) pages) * pagesize) >> 10)
/* Automatically set in init() */
static int pagesize = 4096;
static int numproc = 1;
/* Structs for holding our info */
static struct sysinfo si;
static struct rminfo mi;
void init(void) {
/* Get page size */
pagesize = getpagesize(); /* Alternatively, one could use sysmp(MP_PGSIZE) */
/* Get the number of processors in the system */
numproc = sysmp(MP_NPROCS);
/* Clear the structs */
memset(&si, 0, sizeof(struct sysinfo));
memset(&mi, 0, sizeof(struct rminfo));
}
void display_data(void) {
register int i, ret;
printf("%d CPU(s); Page size = %d bytes\n", numproc, pagesize);
/* Get memory info into the [mi] struct */
ret = sysmp(MP_SAGET, MPSA_RMINFO, &mi, sizeof(struct rminfo));
/* Bail out on error */
if (ret != 0) {
fprintf(stderr, "sysmp(MP_SAGET, MPSA_RMINFO) returned %d\n", ret);
fprintf(stderr, "ERROR: %s\n", strerror(errno));
exit(1);
}
printf("Memory: %d MB free, %d MB used; %d MB total\n",
(unsigned int)(p2kb(mi.freemem) / 1024),
(unsigned int)((p2kb(mi.physmem) - p2kb(mi.freemem)) / 1024),
(unsigned int)(p2kb(mi.physmem) / 1024)
);
/*
* Show loads for every processor
* If you want to get the combined loads for all processors in the system,
* just use MP_SAGET instead of MP_SAGET1 and remove the last parameter.
*/
for (i = 0; i < numproc; i++) {
/* Get info for processor [i] into the [si] struct */
ret = sysmp(MP_SAGET1, MPSA_SINFO, &si, sizeof(struct sysinfo), i);
/* Bail out on error */
if (ret != 0) {
fprintf(stderr,
"sysmp(MP_SAGET1, MPSA_SINFO) returned %d for processor %d\n",
ret, i);
fprintf(stderr, "ERROR: %s\n", strerror(errno));
exit(2);
}
printf("CPU%d: %ld user, %ld kern, %ld sxbrk, %ld intr, %ld idle, %ld wait\n",
i, si.cpu[CPU_USER], si.cpu[CPU_KERNEL], si.cpu[CPU_SXBRK],
si.cpu[CPU_INTR], si.cpu[CPU_IDLE], si.cpu[CPU_WAIT]
);
}
}
int main(int argc, char *argv[]) {
init();
display_data();
return 0;
}
It should give an output similar to this:
Code:
Select all
2 CPU(s); Page size = 16384 bytes
Memory: 1104 MB free, 431 MB used; 1536 MB total
CPU0: 282134 user, 26628 kern, 0 sxbrk, 2928 intr, 2024597 idle, 3504 wait
CPU1: 86504 user, 21074 kern, 0 sxbrk, 1404 intr, 2228580 idle, 2201 wait
Hope that will be of some use to your project
EDIT: Just wanted to add this: to get percentages, you will probably have to gather the data for all processors, usleep() for some time (like maybe half a second, or a second), gather the loads again and compute the percentages from that.
At least that seems to be what `top` and the like do.