Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Copyright (c) 2015 cmm2
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
- #define _GNU_SOURCE
- #include <assert.h>
- #include <errno.h>
- #include <error.h>
- #include <math.h>
- #include <stddef.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/user.h>
- struct statm {
- size_t size;
- size_t resident;
- size_t shared;
- size_t text;
- size_t lib;
- size_t data;
- size_t dt;
- };
- void
- read_statm(pid_t pid, struct statm* s) {
- FILE *file = NULL;
- char *line = NULL;
- char *path = NULL;
- size_t line_len = 0;
- assert(pid > 0);
- assert(s != NULL);
- if (asprintf(&path, "/proc/%i/statm", pid) == -1)
- error(1, errno, "failed to allocate /proc/%i/statm string", pid);
- file = fopen(path, "r");
- if (file == NULL)
- error(1, errno, "failed to open %s", path);
- if (getline(&line, &line_len, file) == -1)
- error(1, errno, "failed to read %s", path);
- if (sscanf(line, "%zu %zu %zu %zu %zu %zu %zu", &s->size, &s->resident,
- &s->shared, &s->text, &s->lib, &s->data, &s->dt) != 7)
- error(1, errno, "failed to parse %s", path);
- free(line);
- fclose(file);
- free(path);
- }
- pid_t
- pid_from_string(char const* str) {
- pid_t pid;
- assert(str != NULL);
- errno = 0;
- pid = strtol(str, NULL, 10);
- if (errno != 0)
- error(1, errno, "failed to parse pid %s", str);
- if (pid < 1)
- error(1, 0, "pid out of range: %s", str);
- return pid;
- }
- double
- size_round(size_t n, char const** suffix) {
- assert(suffix != NULL);
- if (n < 1.024E3) {
- *suffix = "B";
- return n/1E0;
- }
- if (n < 1.048576E6) {
- *suffix = "KiB";
- return n/1.024E3;
- }
- if (n < 1.073741824E9) {
- *suffix = "MiB";
- return n/1.048576E6;
- }
- if (n < 1.099511627776E12) {
- *suffix = "GiB";
- return n/1.073741824E9;
- }
- if (n < 1.125899906842624E15) {
- *suffix = "TiB";
- return n/1.099511627776E12;
- }
- if (n < 1.152921504606846976E18) {
- *suffix = "PiB";
- return n/1.125899906842624E15;
- }
- error(1, 0, "size out of range");
- return 0;
- }
- void
- print_statm_mem(struct statm const* s) {
- double n;
- char const* suffix = NULL;
- assert(s != NULL);
- assert(s->resident > s->shared);
- printf("%-19s %-19s %-19s %-19s\n", "Private", "Virtual", "Resident",
- "Shared");
- n = size_round((s->resident - s->shared) * PAGE_SIZE, &suffix);
- printf("%-6.2lf %-13s", n, suffix);
- n = size_round(s->size * PAGE_SIZE, &suffix);
- printf("%-6.2lf %-13s", n, suffix);
- n = size_round(s->resident * PAGE_SIZE, &suffix);
- printf("%-6.2lf %-13s", n, suffix);
- n = size_round(s->shared * PAGE_SIZE, &suffix);
- printf("%-6.2lf %-13s\n", n, suffix);
- }
- int main(int argc, char const* argv[]) {
- pid_t pid;
- struct statm s = {0};
- if (argc < 2)
- error(1, 0, "pid argument missing");
- pid = pid_from_string(argv[1]);
- read_statm(pid, &s);
- print_statm_mem(&s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement