You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
#ifndef _MEM_MONITOR_H_
|
|
#define _MEM_MONITOR_H_
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/resource.h>
|
|
|
|
// 获取当前进程 RSS (物理内存) 使用量,单位 MB
|
|
static inline long getRSS_KB()
|
|
{
|
|
long rss = 0;
|
|
FILE *fp = fopen("/proc/self/status", "r");
|
|
if (!fp) return -1;
|
|
|
|
char line[256];
|
|
while (fgets(line, sizeof(line), fp))
|
|
{
|
|
if (strncmp(line, "VmRSS:", 6) == 0)
|
|
{
|
|
// 格式: "VmRSS: 12345 kB"
|
|
const char *p = line + 6;
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
rss = atol(p);
|
|
break;
|
|
}
|
|
}
|
|
fclose(fp);
|
|
return rss;
|
|
}
|
|
|
|
// 获取当前进程内存使用,打印格式化日志
|
|
static inline void printMemUsage(const char *tag, const char *extra)
|
|
{
|
|
long rss = getRSS_KB();
|
|
struct rusage usage;
|
|
getrusage(RUSAGE_SELF, &usage);
|
|
printf("[MEM] %-30s | VmRSS: %6ld MB | maxRSS: %6ld MB | %s\n",
|
|
tag, rss / 1024, usage.ru_maxrss / 1024, extra ? extra : "");
|
|
}
|
|
|
|
// 获取产品检测 pipeline 队列深度信息快照
|
|
#define MEM_LOG(tag, fmt, ...) \
|
|
do { \
|
|
long _rss = getRSS_KB(); \
|
|
printf("[MEM] %-30s | VmRSS: %6ld MB | " fmt "\n", tag, _rss / 1024, ##__VA_ARGS__); \
|
|
} while(0)
|
|
|
|
#endif // _MEM_MONITOR_H_
|