2010-06-24 08:35:12 +05:30
|
|
|
/*
|
|
|
|
smemcap - a tool for meaningful memory reporting
|
|
|
|
|
|
|
|
Copyright 2008-2009 Matt Mackall <mpm@selenic.com>
|
|
|
|
|
|
|
|
This software may be used and distributed according to the terms of
|
|
|
|
the GNU General Public License version 2 or later, incorporated
|
|
|
|
herein by reference.
|
|
|
|
*/
|
|
|
|
//config:config SMEMCAP
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "smemcap (2.5 kb)"
|
2010-06-24 08:35:12 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: smemcap is a tool for capturing process data for smem,
|
|
|
|
//config: a memory usage statistic tool.
|
2010-06-24 08:35:12 +05:30
|
|
|
|
2017-09-18 19:58:43 +05:30
|
|
|
//applet:IF_SMEMCAP(APPLET(smemcap, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SMEMCAP) += smemcap.o
|
|
|
|
|
2010-06-24 08:35:12 +05:30
|
|
|
#include "libbb.h"
|
2011-09-22 16:15:14 +05:30
|
|
|
#include "bb_archive.h"
|
2010-06-24 08:35:12 +05:30
|
|
|
|
|
|
|
struct fileblock {
|
|
|
|
struct fileblock *next;
|
2010-06-26 21:51:36 +05:30
|
|
|
char data[TAR_BLOCK_SIZE];
|
2010-06-24 08:35:12 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static void writeheader(const char *path, struct stat *sb, int type)
|
|
|
|
{
|
2010-06-26 21:51:36 +05:30
|
|
|
struct tar_header_t header;
|
2010-06-24 08:35:12 +05:30
|
|
|
|
2010-06-26 21:51:36 +05:30
|
|
|
memset(&header, 0, TAR_BLOCK_SIZE);
|
2010-06-24 08:35:12 +05:30
|
|
|
strcpy(header.name, path);
|
|
|
|
sprintf(header.mode, "%o", sb->st_mode & 0777);
|
|
|
|
/* careful to not overflow fields! */
|
|
|
|
sprintf(header.uid, "%o", sb->st_uid & 07777777);
|
|
|
|
sprintf(header.gid, "%o", sb->st_gid & 07777777);
|
|
|
|
sprintf(header.size, "%o", (unsigned)sb->st_size);
|
|
|
|
sprintf(header.mtime, "%llo", sb->st_mtime & 077777777777LL);
|
|
|
|
header.typeflag = type;
|
2021-08-20 21:28:49 +05:30
|
|
|
chksum_and_xwrite_tar_header(STDOUT_FILENO, &header);
|
2010-06-24 08:35:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void archivefile(const char *path)
|
|
|
|
{
|
|
|
|
struct fileblock *start, *cur;
|
|
|
|
struct fileblock **prev = &start;
|
|
|
|
int fd, r;
|
|
|
|
unsigned size = 0;
|
|
|
|
struct stat s;
|
|
|
|
|
|
|
|
/* buffer the file */
|
2018-08-30 15:19:51 +05:30
|
|
|
fd = open(path, O_RDONLY);
|
2018-08-26 21:07:30 +05:30
|
|
|
if (fd == -1) {
|
|
|
|
/* skip vanished processes between dir listing and traversal */
|
|
|
|
return;
|
|
|
|
}
|
2010-06-24 08:35:12 +05:30
|
|
|
do {
|
|
|
|
cur = xzalloc(sizeof(*cur));
|
|
|
|
*prev = cur;
|
|
|
|
prev = &cur->next;
|
2010-06-26 21:51:36 +05:30
|
|
|
r = full_read(fd, cur->data, TAR_BLOCK_SIZE);
|
2010-06-24 08:35:12 +05:30
|
|
|
if (r > 0)
|
|
|
|
size += r;
|
2010-06-26 21:51:36 +05:30
|
|
|
} while (r == TAR_BLOCK_SIZE);
|
2010-06-24 08:35:12 +05:30
|
|
|
|
|
|
|
/* write archive header */
|
|
|
|
fstat(fd, &s);
|
|
|
|
close(fd);
|
|
|
|
s.st_size = size;
|
|
|
|
writeheader(path, &s, '0');
|
|
|
|
|
|
|
|
/* dump file contents */
|
2010-06-26 21:51:36 +05:30
|
|
|
for (cur = start; (int)size > 0; size -= TAR_BLOCK_SIZE) {
|
|
|
|
xwrite(STDOUT_FILENO, cur->data, TAR_BLOCK_SIZE);
|
2010-06-24 08:35:12 +05:30
|
|
|
start = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
free(start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void archivejoin(const char *sub, const char *name)
|
|
|
|
{
|
|
|
|
char path[sizeof(long long)*3 + sizeof("/cmdline")];
|
|
|
|
sprintf(path, "%s/%s", sub, name);
|
|
|
|
archivefile(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
//usage:#define smemcap_trivial_usage ">SMEMDATA.TAR"
|
|
|
|
//usage:#define smemcap_full_usage "\n\n"
|
|
|
|
//usage: "Collect memory usage data in /proc and write it to stdout"
|
|
|
|
|
|
|
|
int smemcap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int smemcap_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
|
|
|
{
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *de;
|
|
|
|
|
|
|
|
xchdir("/proc");
|
|
|
|
d = xopendir(".");
|
|
|
|
|
|
|
|
archivefile("meminfo");
|
|
|
|
archivefile("version");
|
|
|
|
while ((de = readdir(d)) != NULL) {
|
|
|
|
if (isdigit(de->d_name[0])) {
|
|
|
|
struct stat s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
|
|
s.st_mode = 0555;
|
|
|
|
writeheader(de->d_name, &s, '5');
|
|
|
|
archivejoin(de->d_name, "smaps");
|
|
|
|
archivejoin(de->d_name, "cmdline");
|
|
|
|
archivejoin(de->d_name, "stat");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-22 02:14:47 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
closedir(d);
|
|
|
|
|
2010-06-24 08:35:12 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|