/* * splinter -- the self plugin: the session's own resource use. * * The numbers come from /proc, so the test proves the shape and that both the * widget and /self answer; the exact values belong to the machine. */ #include #include #include #include #include #include #include "splinter.h" #include "splinter_plugin.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static void nap_ms(long ms) { struct timespec ts; ts.tv_sec = ms / 1000; ts.tv_nsec = (ms % 1000) * 1000000L; nanosleep(&ts, NULL); } int main(void) { struct sp_registry reg; struct sp_agent ag; struct sp_host *host; struct sp_buf err, out; char snap[2048]; pid_t kid; memset(®, 0, sizeof(reg)); memset(&ag, 0, sizeof(ag)); if (sp_buf_init(&err) != 0 || sp_buf_init(&out) != 0) return 1; host = sp_host_new(®, &ag, NULL); CHECK(host != NULL); CHECK(sp_plugin_load(host, "plugins/self/self.so", &err) == 0); CHECK(sp_registry_find(®, "session_usage") != NULL); /* * A child of ours must be counted. The scan of /proc used to stop * after 256 entries; on a machine with more processes it never got * to our own pid and saw no children at all. */ kid = fork(); if (kid == 0) { nap_ms(10000); _exit(0); } CHECK(kid > 0); /* the widget samples on its first tick and every two seconds */ nap_ms(2400); snap[0] = '\0'; CHECK(sp_host_widget_snapshot(host, snap, sizeof(snap)) > 0); CHECK(strstr(snap, "self: ") != NULL); CHECK(strstr(snap, "memory") != NULL); CHECK(strstr(snap, "threads") != NULL); CHECK(strstr(snap, "open files") != NULL); CHECK(strstr(snap, "children") != NULL); CHECK(strstr(snap, "no children") == NULL); if (kid > 0) { (void)kill(kid, SIGKILL); (void)waitpid(kid, NULL, 0); } /* /self and the tool carry the same numbers, with the child count */ sp_buf_clear(&out); CHECK(sp_host_command(host, "self", "", &out) == 0); CHECK(strstr(out.p, "memory (RSS):") != NULL); CHECK(strstr(out.p, "threads:") != NULL); CHECK(strstr(out.p, "children:") != NULL); CHECK(strstr(out.p, "cpu:") != NULL); sp_host_free(host); sp_buf_free(&err); sp_buf_free(&out); sp_registry_free(®); if (failures == 0) printf("test_self: ok\n"); return failures == 0 ? 0 : 1; }