0

如何将je_malloc_stats_print()结果写入文件而不是stderr?现在我能做的只有 je_malloc_stats_print(NULL, NULL, NULL) 到 stderr;

4

2 回答 2

3

第一个参数是回调函数指针,第二个参数用于向回调传递数据。您可以使用这些参数来实现对文件的写入。从jemalloc 手册页

void malloc_stats_print( void (*write_cb) (void *, const char *) , void *cbopaque, const char *opts);

于 2013-08-19T13:51:49.003 回答
0
/*Actually The default function, it uses inside print the stat data on STDERR output stream. To print it to a file, It needs to provide a callback function, which it will call to print the buf stream into. */

int g_fd;

void print_my_jemalloc_data(void *opaque, const char *buf);

int main()

{

  int fd = open("heap_stats.out,O_CREAT|O_WRONLY,0666);

  g_fd = fd;

  malloc_stats_print(print_my_jemalloc_data,NULL,NULL); 

  /*Passing my callback routine which jemalloc will use internally to print data into*/

return 0;

}

void print_my_jemalloc_data(void *opaque,const char *buf)
{
 write(g_fd,buf,strlen(buf));`enter code here`
}

您可以使用相同的签名替换您的函数,并在 malloc_stat_print API 处替换第一个参数回调函数。它将在 buf 上传递,您可以将其打印在您之前打开的定义文件流中。

于 2017-09-06T13:28:23.540 回答