Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | smemcap - a tool for meaningful memory reporting |
| 3 | |
| 4 | Copyright 2008-2009 Matt Mackall <mpm@selenic.com> |
| 5 | |
| 6 | This software may be used and distributed according to the terms of |
| 7 | the GNU General Public License version 2 or later, incorporated |
| 8 | herein by reference. |
| 9 | */ |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 10 | //config:config SMEMCAP |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 11 | //config: bool "smemcap (2.5 kb)" |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 12 | //config: default y |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: smemcap is a tool for capturing process data for smem, |
| 15 | //config: a memory usage statistic tool. |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 16 | |
Denys Vlasenko | 0c4dbd4 | 2017-09-18 16:28:43 +0200 | [diff] [blame] | 17 | //applet:IF_SMEMCAP(APPLET(smemcap, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 18 | |
| 19 | //kbuild:lib-$(CONFIG_SMEMCAP) += smemcap.o |
| 20 | |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 21 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 22 | #include "bb_archive.h" |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 23 | |
| 24 | struct fileblock { |
| 25 | struct fileblock *next; |
Denys Vlasenko | 52827e3 | 2010-06-26 18:21:36 +0200 | [diff] [blame] | 26 | char data[TAR_BLOCK_SIZE]; |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | static void writeheader(const char *path, struct stat *sb, int type) |
| 30 | { |
Denys Vlasenko | 52827e3 | 2010-06-26 18:21:36 +0200 | [diff] [blame] | 31 | struct tar_header_t header; |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 32 | |
Denys Vlasenko | 52827e3 | 2010-06-26 18:21:36 +0200 | [diff] [blame] | 33 | memset(&header, 0, TAR_BLOCK_SIZE); |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 34 | strcpy(header.name, path); |
| 35 | sprintf(header.mode, "%o", sb->st_mode & 0777); |
| 36 | /* careful to not overflow fields! */ |
| 37 | sprintf(header.uid, "%o", sb->st_uid & 07777777); |
| 38 | sprintf(header.gid, "%o", sb->st_gid & 07777777); |
| 39 | sprintf(header.size, "%o", (unsigned)sb->st_size); |
| 40 | sprintf(header.mtime, "%llo", sb->st_mtime & 077777777777LL); |
| 41 | header.typeflag = type; |
Denys Vlasenko | 62d5a1e | 2021-08-20 17:58:49 +0200 | [diff] [blame] | 42 | chksum_and_xwrite_tar_header(STDOUT_FILENO, &header); |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void archivefile(const char *path) |
| 46 | { |
| 47 | struct fileblock *start, *cur; |
| 48 | struct fileblock **prev = &start; |
| 49 | int fd, r; |
| 50 | unsigned size = 0; |
| 51 | struct stat s; |
| 52 | |
| 53 | /* buffer the file */ |
Denys Vlasenko | 270becc | 2018-08-30 11:49:51 +0200 | [diff] [blame] | 54 | fd = open(path, O_RDONLY); |
Andre Goddard Rosa | 9a2621a | 2018-08-26 17:37:30 +0200 | [diff] [blame] | 55 | if (fd == -1) { |
| 56 | /* skip vanished processes between dir listing and traversal */ |
| 57 | return; |
| 58 | } |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 59 | do { |
| 60 | cur = xzalloc(sizeof(*cur)); |
| 61 | *prev = cur; |
| 62 | prev = &cur->next; |
Denys Vlasenko | 52827e3 | 2010-06-26 18:21:36 +0200 | [diff] [blame] | 63 | r = full_read(fd, cur->data, TAR_BLOCK_SIZE); |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 64 | if (r > 0) |
| 65 | size += r; |
Denys Vlasenko | 52827e3 | 2010-06-26 18:21:36 +0200 | [diff] [blame] | 66 | } while (r == TAR_BLOCK_SIZE); |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 67 | |
| 68 | /* write archive header */ |
| 69 | fstat(fd, &s); |
| 70 | close(fd); |
| 71 | s.st_size = size; |
| 72 | writeheader(path, &s, '0'); |
| 73 | |
| 74 | /* dump file contents */ |
Denys Vlasenko | 52827e3 | 2010-06-26 18:21:36 +0200 | [diff] [blame] | 75 | for (cur = start; (int)size > 0; size -= TAR_BLOCK_SIZE) { |
| 76 | xwrite(STDOUT_FILENO, cur->data, TAR_BLOCK_SIZE); |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 77 | start = cur; |
| 78 | cur = cur->next; |
| 79 | free(start); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void archivejoin(const char *sub, const char *name) |
| 84 | { |
| 85 | char path[sizeof(long long)*3 + sizeof("/cmdline")]; |
| 86 | sprintf(path, "%s/%s", sub, name); |
| 87 | archivefile(path); |
| 88 | } |
| 89 | |
| 90 | //usage:#define smemcap_trivial_usage ">SMEMDATA.TAR" |
| 91 | //usage:#define smemcap_full_usage "\n\n" |
| 92 | //usage: "Collect memory usage data in /proc and write it to stdout" |
| 93 | |
| 94 | int smemcap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 95 | int smemcap_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 96 | { |
| 97 | DIR *d; |
| 98 | struct dirent *de; |
| 99 | |
| 100 | xchdir("/proc"); |
| 101 | d = xopendir("."); |
| 102 | |
| 103 | archivefile("meminfo"); |
| 104 | archivefile("version"); |
| 105 | while ((de = readdir(d)) != NULL) { |
| 106 | if (isdigit(de->d_name[0])) { |
| 107 | struct stat s; |
| 108 | memset(&s, 0, sizeof(s)); |
| 109 | s.st_mode = 0555; |
| 110 | writeheader(de->d_name, &s, '5'); |
| 111 | archivejoin(de->d_name, "smaps"); |
| 112 | archivejoin(de->d_name, "cmdline"); |
| 113 | archivejoin(de->d_name, "stat"); |
| 114 | } |
| 115 | } |
| 116 | |
Alexander Shishkin | cbfeaac | 2010-10-21 23:44:47 +0300 | [diff] [blame] | 117 | if (ENABLE_FEATURE_CLEAN_UP) |
| 118 | closedir(d); |
| 119 | |
Denys Vlasenko | a091d45 | 2010-06-24 05:05:12 +0200 | [diff] [blame] | 120 | return EXIT_SUCCESS; |
| 121 | } |