免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1618 | 回复: 3
打印 上一主题 下一主题

1s查下proc文件,会不会很耗系统资源啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-23 19:49 |只看该作者 |倒序浏览
rt,我想实时监控内存的使用情况,特别是在系统资源紧张的时候

论坛徽章:
0
2 [报告]
发表于 2009-04-23 20:23 |只看该作者
和 proc 有什么关系?
unsigned int get_free_mem()
{
    struct sysinfo info;
    sysinfo(&info);
                                                                                                                              
    /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
    if (info.mem_unit==0) {
            info.mem_unit=1;
    }
    if ( info.mem_unit == 1 ) {
            info.mem_unit=1024;
            info.totalram/=info.mem_unit;
            info.freeram/=info.mem_unit;
    } else {
        info.mem_unit/=1024;
        info.totalram*=info.mem_unit;
        info.freeram*=info.mem_unit;
    }
    return info.freeram;
}

论坛徽章:
0
3 [报告]
发表于 2009-04-24 08:58 |只看该作者
static int meminfo_read_proc(char *page, char **start, off_t off,
                                 int count, int *eof, void *data)
{
        struct sysinfo i;
        int len;
        struct page_state ps;
        unsigned long inactive;
        unsigned long active;
        unsigned long free;
        unsigned long committed;
        unsigned long allowed;
        struct vmalloc_info vmi;

        get_page_state(&ps);
        get_zone_counts(&active, &inactive, &free);

/*
* display in kilobytes.
*/
#define K(x) ((x) << (PAGE_SHIFT - 10))
        si_meminfo(&i);
        si_swapinfo(&i);
        committed = atomic_read(&vm_committed_space);
        allowed = ((totalram_pages - hugetlb_total_pages())
                * sysctl_overcommit_ratio / 100) + total_swap_pages;

        get_vmalloc_info(&vmi);

        /*
         * Tagged format, for easy grepping and expansion.
         */
        len = sprintf(page,
                "MemTotal:     %8lu kB\n"
                "MemFree:      %8lu kB\n"
                "Buffers:      %8lu kB\n"
                "Cached:       %8lu kB\n"
                "SwapCached:   %8lu kB\n"
                "Active:       %8lu kB\n"
                "Inactive:     %8lu kB\n"
                "HighTotal:    %8lu kB\n"
                "HighFree:     %8lu kB\n"
                "LowTotal:     %8lu kB\n"
                "LowFree:      %8lu kB\n"
                "SwapTotal:    %8lu kB\n"
                "SwapFree:     %8lu kB\n"
                "Dirty:        %8lu kB\n"
                "Writeback:    %8lu kB\n"
                "Mapped:       %8lu kB\n"
                "Slab:         %8lu kB\n"
                "CommitLimit:  %8lu kB\n"
                "Committed_AS: %8lu kB\n"
                "PageTables:   %8lu kB\n"
                "VmallocTotal: %8lu kB\n"
                "VmallocUsed:  %8lu kB\n"
                "VmallocChunk: %8lu kB\n",
                K(i.totalram),
                K(i.freeram),
                K(i.bufferram),
                K(get_page_cache_size()-total_swapcache_pages-i.bufferram),
                K(total_swapcache_pages),
                K(active),
                K(inactive),
                K(i.totalhigh),
                K(i.freehigh),
                K(i.totalram-i.totalhigh),
                K(i.freeram-i.freehigh),
                K(i.totalswap),
                K(i.freeswap),
                K(ps.nr_dirty),
                K(ps.nr_writeback),
                K(ps.nr_mapped),
                K(ps.nr_slab),
                K(allowed),
                K(committed),
                K(ps.nr_page_table_pages),
                VMALLOC_TOTAL >> 10,
                vmi.used >> 10,
                vmi.largest_chunk >> 10
                );

                len += hugetlb_report_meminfo(page + len);

        return proc_calc_metrics(page, start, off, count, eof, len);
#undef K
}
我是通过/proc/meminfo这个来查的,因为对用户来讲,freemem有memfree,buffer,cache这三块。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2009-04-24 10:02 |只看该作者
原帖由 linkejin 于 2009-4-23 19:49 发表
rt,我想实时监控内存的使用情况,特别是在系统资源紧张的时候

测一下不就知道了。

BTW:其实我觉得 1 秒有些没必要。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP