-
-
Notifications
You must be signed in to change notification settings - Fork 57
Report swapinfo #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Report swapinfo #140
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,17 +16,24 @@ int used_mem_change_threshold; | |
| int delay; | ||
| int usr1_received; | ||
|
|
||
| const char *parse(const char *meminfo_buf, const char* dom_current_buf) | ||
| typedef struct { | ||
| char *mem; | ||
| char *swap; | ||
| } UsedMem; | ||
|
|
||
| UsedMem parse(const char *meminfo_buf, const char* dom_current_buf) | ||
| { | ||
| const char *ptr = meminfo_buf; | ||
| static char outbuf[4096]; | ||
| static char used_mem_buf[4096]; | ||
| static char used_swap_buf[4096]; | ||
| long long val; | ||
| int len; | ||
| int ret; | ||
| long long MemTotal = 0, MemFree = 0, Buffers = 0, Cached = 0, SwapTotal = | ||
| 0, SwapFree = 0; | ||
| unsigned long long key; | ||
| long long used_mem, used_mem_diff; | ||
| long long used_swap; | ||
| int nitems = 0; | ||
|
|
||
| while (nitems != (1<<6)-1 || !*ptr) { | ||
|
|
@@ -68,7 +75,8 @@ const char *parse(const char *meminfo_buf, const char* dom_current_buf) | |
| used_mem = | ||
| MemTotal - Buffers - Cached - MemFree + SwapTotal - SwapFree; | ||
| if (used_mem < 0) | ||
| return NULL; | ||
| return (UsedMem){ .mem = NULL, .swap = NULL }; | ||
| used_swap = SwapTotal - SwapFree; | ||
|
|
||
| used_mem_diff = used_mem - prev_used_mem; | ||
| if (used_mem_diff < 0) | ||
|
|
@@ -78,10 +86,13 @@ const char *parse(const char *meminfo_buf, const char* dom_current_buf) | |
| || (used_mem > prev_used_mem && used_mem / 10 > (MemTotal+12) / 13 | ||
| && used_mem_diff > used_mem_change_threshold/2)) { | ||
| prev_used_mem = used_mem; | ||
| sprintf(outbuf, "%lld", used_mem); | ||
| return outbuf; | ||
| sprintf(used_mem_buf, "%lld", used_mem); | ||
| sprintf(used_swap_buf, "%lld", used_swap); | ||
|
Comment on lines
+89
to
+90
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant calls. |
||
| snprintf(used_mem_buf, sizeof used_mem_buf, "%lld", used_mem); | ||
| snprintf(used_swap_buf, sizeof used_swap_buf, "%lld", used_swap); | ||
| return (UsedMem){ .mem = used_mem_buf, .swap = used_swap_buf }; | ||
| } | ||
| return NULL; | ||
| return (UsedMem){ .mem = NULL, .swap = NULL }; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Report swap change if above threshold despite memory being below its threshold. |
||
| } | ||
|
|
||
| void usage(void) | ||
|
|
@@ -94,10 +105,14 @@ void usage(void) | |
| exit(1); | ||
| } | ||
|
|
||
| void send_to_qmemman(struct xs_handle *xs, const char *data) | ||
| void send_to_qmemman(struct xs_handle *xs, const char *mem, const char *swap) | ||
| { | ||
| if (!xs_write(xs, XBT_NULL, "memory/meminfo", data, strlen(data))) { | ||
| syslog(LOG_DAEMON | LOG_ERR, "error writing xenstore ?"); | ||
| if (!xs_write(xs, XBT_NULL, "memory/meminfo", mem, strlen(mem))) { | ||
| syslog(LOG_DAEMON | LOG_ERR, "error writing meminfo to xenstore ?"); | ||
| exit(1); | ||
| } | ||
| if (!xs_write(xs, XBT_NULL, "memory/swapinfo", swap, strlen(swap))) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: this can be merged only after core-admin part, which sets necessary xenstore access. |
||
| syslog(LOG_DAEMON | LOG_ERR, "error writing swapinfo to xenstore ?"); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
@@ -121,7 +136,6 @@ static void update(struct xs_handle *xs, int meminfo_fd, int dom_current_fd) | |
| char dom_current_buf[32]; | ||
| char dom_current_buf2[32]; | ||
| char meminfo_buf[4096]; | ||
| const char *meminfo_data; | ||
|
|
||
| pread0_string(dom_current_fd, dom_current_buf, sizeof(dom_current_buf)); | ||
|
|
||
|
|
@@ -140,9 +154,9 @@ static void update(struct xs_handle *xs, int meminfo_fd, int dom_current_fd) | |
| break; | ||
| } | ||
|
|
||
| meminfo_data = parse(meminfo_buf, dom_current_buf); | ||
| if (meminfo_data) | ||
| send_to_qmemman(xs, meminfo_data); | ||
| UsedMem meminfo_data = parse(meminfo_buf, dom_current_buf); | ||
| if (meminfo_data.mem && meminfo_data.mem[0]) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| send_to_qmemman(xs, meminfo_data.mem, meminfo_data.swap); | ||
| } | ||
|
|
||
| int main(int argc, char **argv) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW the usual method in C for returning more results is to either return a pointer to a structure (usually dynamically allocated via malloc), or add output parameters (pointers to where function should write result). In case of a simple structure like this, compiler will do the latter for you here, so it can stay this way, but keep in mind it wont be efficient code in more complex cases.