ksar銆乻ar鍙婄浉鍏冲唴鏍哥煡璇嗙偣瑙f瀽

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ksar銆乻ar鍙婄浉鍏冲唴鏍哥煡璇嗙偣瑙f瀽相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/code' title='code'>code   current   esc   structure   char   socket   creat   export   event   

鍏抽敭璇嶏細sar銆乻adc銆乲sar銆?proc/stat銆?proc/cpuinfo銆?proc/meminfo銆?proc/diskstats銆?/p>

鍦ㄤ箣鍓嶆湁绠€鍗曚粙缁嶈繃sar/ksar锛屾渶杩戝湪浣跨敤涓劅瑙夐渶瑕佸啀娣卞叆浜嗚В涓€涓嬨€?/p>

ksar/sar浠庡唴鏍搁噰闆嗘暟鎹紝骞惰緭鍑哄彲璇绘€ф暟鎹€傚垎鏋愮浉鍏虫簮鐮侊紝鏈夊姪浜庣煡閬撴暟鎹潵榫欏幓鑴夈€?-------------------------------------1. sar婧愮爜姒傝

ksar鏄剧ずsar/sadc鑾峰彇鐨勬暟鎹紝骞跺浘褰㈠寲鏄剧ず銆傛暟鎹粠鍐呮牳鑺傜偣锛屽埌sadc/sar杞崲锛屽啀鍒発sar鏄剧ず銆?---------------------------2. ksar澶勭悊娴佺▼

瀵圭収ksar姣忓紶鍥捐〃锛岀劧鍚巗ar/sadc瀵瑰簲鐨勯噰闆嗚浆鎹紝鍐嶅埌鍐呮牳姣忎釜鏁版嵁椤瑰惈涔夎В鏋愩€?-------------------------------------------3. ksar瑙h

鏈熸湜鏄粠ksar涓婄殑鍥捐〃鑳藉搴斿埌鍐呮牳鐨勪唬鐮侊紝鏄庣櫧杩欎簺鍥捐〃鏁版嵁鏍规簮銆?/p>

1. sar婧愮爜姒傝

sar浣滀负sysstat涓€閮ㄥ垎锛岀浉鍏崇殑宸ュ叿杩樺寘鎷瑂adc銆乻a1銆乻a2銆?/p>

sa1璐熻矗鏀堕泦骞跺瓨鍌ㄦ瘡澶╃郴缁熷姩鎬佷俊鎭埌涓€涓簩杩涘埗鐨勬枃浠朵腑锛宻a1鏄痵adc鎵€娑夊強鐨勭▼搴忓墠绔▼搴忋€傞€氬父鐢辫鍒掍换鍔″伐鍏穋ron鏉ヨ皟鐢ㄣ€傛墦寮€sa1鏂囦欢涓嶉毦鐪嬪嚭灏辨槸璋冪敤sadc浠婃閭d釜閲囬泦鏁版嵁銆?/p>

sa2灏辨槸璋冪敤sar鍛戒护锛屽皢褰撴棩浜岃繘鍒舵棩蹇楁枃浠舵暟鎹瓨鍌ㄥ埌鏂囨湰鏂囦欢涓€?/p>

sadc鏄郴缁熷姩鎬佹暟鎹敹闆嗗伐鍏凤紝鏀堕泦鐨勬暟鎹鍐欏叆涓€涓簩杩涘埗鏂囦欢涓紝瀹冩槸sar宸ュ叿鍚庣銆?/p>

sar璐熻矗瑙f瀽sadc淇濆瓨鐨勬暟鎹紝骞舵樉绀哄嚭鏉ャ€?/p>

褰撲娇鐢╯ar杩涜鏁版嵁缁熻鐨勬椂鍊欙紝閫氳繃pstree `pidof sar`锛屽彲浠ョ湅鍑簊ar璋冪敤浜唖adc銆?/p>

sar鈹€鈹€鈹€sadc

 

1.1 sadc淇℃伅閲囨牱

sadc鍏ュ彛鍦╯adc.c涓紝涓昏鏄В鏋愬弬鏁般€佸惎鍔ㄤ竴涓猧nterval alarm銆乺w_sa_stat_loop()璇诲彇鏁版嵁銆?/p>

閫氳繃alarm瑙﹀彂SIGALRM瀹炵幇鍛ㄦ湡鎬ц鍙栵紝SIGINT鍋滄璇诲彇銆?/p>

int main(int argc, char **argv)
{
    int opt = 0;
    char ofile[MAX_FILE_LEN], sa_dir[MAX_FILE_LEN];
    int stdfd = 0, ofd = -1;
    int restart_mark;
    long count = 0;

    /* Get HZ */
    get_HZ();

    /* Compute page shift in kB */
    get_kb_shift();

    ofile[0] = sa_dir[0] = comment[0] = 鈥?/span>鈥?/span>;

...-----------------------------------------------------------瑙f瀽鍙傛暟骞惰繘琛岄厤缃€?
    /* Set a handler for SIGALRM */
    memset(&alrm_act, 0, sizeof(alrm_act));
    alrm_act.sa_handler = alarm_handler;
    sigaction(SIGALRM, &alrm_act, NULL);
    alarm(interval);------------------------------------------鍚姩涓€涓猘larm锛岃秴鏃舵湭interval锛屽苟閫氳繃鍦╝larm_handler()涓噸鏂拌捣涓€涓猘larm瀹炵幇鍛ㄦ湡鎬у鐞嗐€?
    /* Main loop */
    rw_sa_stat_loop(count, stdfd, ofd, ofile, sa_dir);

#ifdef HAVE_SENSORS
    /* Cleanup sensors */
    sensors_cleanup();
#endif /* HAVE_SENSORS */

    /* Free structures */
    sa_sys_free();

    return 0;
}

rw_sa_stat_loop()鏄暣涓猻adc鐨勬牳蹇冨惊鐜紝杩欓噷浠庝釜sysfs璇诲彇淇℃伅锛屽苟鎻愬彇鍏抽敭淇℃伅锛岀劧鍚庝繚瀛樸€?/p>

void rw_sa_stat_loop(long count, int stdfd, int ofd, char ofile[],
             char sa_dir[])
{
    int do_sa_rotat = 0;
    unsigned int save_flags;
    char new_ofile[MAX_FILE_LEN] = "";
    struct tm rectime = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};

    /* Set a handler for SIGINT */
    memset(&int_act, 0, sizeof(int_act));
    int_act.sa_handler = int_handler;
    sigaction(SIGINT, &int_act, NULL);
/* Main loop */
    do {

        reset_stats();
...
        /* Read then write stats */
        read_stats();--------------------------------------------------------閬嶅巻act[]涓墍鏈夌殑struct activity锛岃繘琛岄噰鏍枫€?
        if (stdfd >= 0) {
            save_flags = flags;
            flags &= ~S_F_LOCK_FILE;
            write_stats(stdfd);----------------------------------------------閫氳繃鏍囧噯杈撳嚭鏂囦欢鎵撳嵃淇℃伅銆?/span>
            flags = save_flags;
        }

        /* If the record type was R_LAST_STATS, tag it R_STATS before writing it */
        record_hdr.record_type = R_STATS;
        if (ofile[0]) {
            write_stats(ofd);------------------------------------------------灏嗙粨鏋滃啓鍒版寚瀹氭枃浠朵腑銆?/span>
        }
...
        fflush(stdout);

        if (count > 0) {
            count--;
        }

        if (count) {
            /* Wait for a signal (probably SIGALRM or SIGINT) */
            pause();----------------------------------------------------------姝ゅ鍜宎larm()閰嶅悎杈惧埌鍛ㄦ湡鎬ч噰鏍风殑鏁堟灉銆?/span>
        }

        if (sigint_caught)----------------------------------------------------濡傛灉鏀跺埌SIGINT淇″彿锛屾彁鍓嶇粓姝㈤噰鏍枫€?            /* SIGINT caught: Stop now */
            break;
...
    }
    while (count);------------------------------------------------------------杈惧埌鎬婚噰鏍锋暟锛屽悓鏍峰仠姝㈤噰鏍枫€?
    /* Close file descriptors if they have actually been used */
    CLOSE(stdfd);
    CLOSE(ofd);
}

read_stats()鏄牳蹇冮噰闆嗘暟鎹嚱鏁帮紝鏍稿績鏁版嵁缁撴瀯寮?a href="#act">act[]銆?/p>

void read_stats(void)
{
    int i;
    __nr_t cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr;

    record_hdr.uptime0 = 0;
    if (cpu_nr > 2) {
        read_uptime(&(record_hdr.uptime0));
    }

    for (i = 0; i < NR_ACT; i++) {
        if (IS_COLLECTED(act[i]->options)) {---------------------------------------閬嶅巻鎵€鏈塧ct[]锛屽鏋渁ct[]涓搴旂殑options鍖呭惈AO_COLLECTED鍒欒繘琛宖_read()銆?            /* Read statistics for current activity */            (*act[i]->f_read)(act[i]);
        }
    }

    if (cpu_nr == 1) {
        record_hdr.uptime0 = record_hdr.uptime;
    }
}

act[]瀛樻斁浜嗘墍鏈夌粺璁′簨浠剁殑struct activity銆?/p>

struct activity *act[NR_ACT] = {
    &cpu_act,
    &pcsw_act,
    &irq_act,
    &swap_act,
    &paging_act,
    &io_act,
    &memory_act,
    &huge_act,
    &ktables_act,
    &queue_act,
    &serial_act,
    &disk_act,
    /* <network> */
    &net_dev_act,
    &net_edev_act,
    &net_nfs_act,
    &net_nfsd_act,
    &net_sock_act,
    &net_ip_act,
    &net_eip_act,
    &net_icmp_act,
    &net_eicmp_act,
    &net_tcp_act,
    &net_etcp_act,
    &net_udp_act,
    &net_sock6_act,
    &net_ip6_act,
    &net_eip6_act,
    &net_icmp6_act,
    &net_eicmp6_act,
    &net_udp6_act,
    &fchost_act,
    &softnet_act,    /* AO_CLOSE_MARKUP */
    /* </network> */
    /* <power-management> */
    &pwr_cpufreq_act,
    &pwr_fan_act,
    &pwr_temp_act,
    &pwr_in_act,
    &pwr_wghfreq_act,
    &pwr_usb_act,        /* AO_CLOSE_MARKUP */
    /* </power-management> */
    &filesystem_act
};

 

1.2 sar鏄剧ず缁熻淇℃伅

read_sadc_stat_bunch()璇诲彇缁熻淇℃伅锛寃rite_stats()璋冪敤姣忎釜struct activity鐨刦_print()鍑芥暟锛寃rite_stats_avg()璋冪敤姣忎釜struct activity鐨刦_print_avg()鍑芥暟銆?/p>

f_print()鍜宖_print_avg()鎴栦粠鏂囦欢涓В鏋愬瓧绗︿覆锛屾垨鍚姩sadc閲囨牱锛岀劧鍚庡啀瑙f瀽銆?/p>

2 ksar澶勭悊娴佺▼

2.1 ksar浠嬬粛

ksar璧勬簮锛?a href="https://sourceforge.net/projects/ksar/files/ksar/" target="_blank">pdf甯姪鏂囨。銆?/p>

ksar涓湅鍒扮殑鍥炬爣鏄粨鏋滐紝杩欎簺鏁版嵁鏄€氳繃sadc閲囬泦锛宻ar瑙f瀽鍑烘潵鐨勩€?/p>

sadc鏄€氳繃璇诲彇sysfs/procfs鑺傜偣鏉ヨ幏鍙栦俊鎭紝杩欎簺鑺傜偣閮芥槸鍐呮牳鎻愪緵鐨勭粺璁′俊鎭€?/p>

sar -o temp.bin 1 600--------------------------------------------------sar灏嗛噰鏍锋暟鎹繚瀛樺湪temp.bin涓€?/p>

LC_ALL=C sar -A -f temp.bin > sar.txt----------------------------灏嗕繚瀛樼殑閲囨牱鏁版嵁temp.bin锛岃浆鎹㈡垚鏇村彲璇绘€у己鐨勬枃鏈枃浠躲€?/p>

鎵€浠sar鐨勬瘡涓€寮犲浘鏍囷紝閮藉搴斾簡鍐呮牳缁熻淇℃伅銆?/p>

sar鏄暟鎹惉杩愭暣鐞嗗伐鍏凤紝ksar鏄浘褰㈠寲宸ュ叿銆?/p>

涓嬮潰瀵规瘡寮犲浘鏍囦粠ksar锛屽埌sar/sadc锛屾渶缁堝埌鍐呮牳涓瘡涓暟鎹€?/p>

 

2.2 ksar鎿嶄綔

2.2.1 鏁版嵁瀵煎叆

閫氳繃Data->Append from a file...浠巘xt涓姞杞芥暟鎹紝杩樻湁鍏朵粬涓ょ鏁版嵁鏉ユ簮鏂瑰紡銆?/p>

鎶€鏈垎浜浘鐗? src=

2.2.2 鏁版嵁瀵煎嚭

濡傛灉瑕佸皢鍥捐〃瀵煎嚭锛屽彲浠ュ湪姣忓紶鍥炬爣涓嬮潰閫夋嫨Export PNG涔嬬被銆?/p>

鎴栬€呴€氳繃Export->Export to PDF...锛岄€夋嫨鎸囧畾閫夐」銆?/p>

鎶€鏈垎浜浘鐗? src=

 

3. ksar瑙f瀽

涓嬮潰缁撳悎ksar鍥捐〃鏉ラ€愰」瑙f瀽銆?/p>

3.1 CPU淇℃伅

cpu_act璇诲彇/proc/stat鑺傜偣锛岃В鏋愬叾涓璫pu淇℃伅锛屽寘鎷悎璁pu淇℃伅浠ュ強鍗曚釜cpu淇℃伅銆?/span>

/proc/stat涓殑淇℃伅鍖呮嫭涓€涓悎璁′互鍙婂涓猚pu鍗曠嫭缁熻淇℃伅銆?/span>

浠?proc/stat涓鍙栫殑淇℃伅閮芥槸浠庡惎鍔ㄤ互鏉ョ殑绱鏃堕棿锛屽湪鍥炬爣涓樉绀虹殑鏄竴涓椂闂存鐨勫樊鍊笺€?/span>

鐒跺悗璁$畻涓嶅悓妯″潡鑰楁椂鍗犳瘮銆?/span>

鎶€鏈垎浜浘鐗? src=

 

void read_stat_cpu(struct stats_cpu *st_cpu, int nbr,
           unsigned long long *uptime, unsigned long long *uptime0)
{
    FILE *fp;
    struct stats_cpu *st_cpu_i;
    struct stats_cpu sc;
    char line[8192];
    int proc_nb;

    if ((fp = fopen(STAT, "r")) == NULL) {----------------------------------------------------/proc/stat
        fprintf(stderr, _("Cannot open %s: %s
"), STAT, strerror(errno));
        exit(2);
    }

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "cpu ", 4)) {------------------------------------------------------缁熻鎬籧pu涓嶅悓绫诲埆鑰楁椂锛岃缁嗕俊鎭弬鑰?a href="https://www.cnblogs.com/arnoldlu/p/9187775.html#system_proc_stat" target="_blank">/proc/stat銆傝繖閲岀殑淇℃伅鍜屽唴鏍镐腑涓€涓€瀵瑰簲銆?            memset(st_cpu, 0, STATS_CPU_SIZE);
            sscanf(line + 5, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
                   &st_cpu->cpu_user,
                   &st_cpu->cpu_nice,
                   &st_cpu->cpu_sys,
                   &st_cpu->cpu_idle,
                   &st_cpu->cpu_iowait,
                   &st_cpu->cpu_hardirq,
                   &st_cpu->cpu_softirq,
                   &st_cpu->cpu_steal,
                   &st_cpu->cpu_guest,
                   &st_cpu->cpu_guest_nice);

            *uptime = st_cpu->cpu_user + st_cpu->cpu_nice    +
                st_cpu->cpu_sys    + st_cpu->cpu_idle    +
                st_cpu->cpu_iowait + st_cpu->cpu_hardirq +
                st_cpu->cpu_steal  + st_cpu->cpu_softirq;
        }

        else if (!strncmp(line, "cpu", 3)) {
...
    }

    fclose(fp);
}

 

3.2 杩涚▼鍒涘缓鍙婂垏鎹?/h2>

pcsw_act璇诲彇/proc/stat鑺傜偣锛岃В鏋愬叾涓殑ctxt鍜宲rocesses淇℃伅銆?/p>

鍚屾牱鐨?proc/stat涓紝缁熻淇℃伅鏄惎鍔ㄤ互鏉ョ殑绱鍊硷紝鍥炬爣涓幇瀹炵殑涓嶅悓鏃堕棿娈电殑宸€笺€?/p>

鎶€鏈垎浜浘鐗? src=

 

void read_stat_pcsw(struct stats_pcsw *st_pcsw)
{
    FILE *fp;
    char line[8192];

    if ((fp = fopen(STAT, "r")) == NULL)
        return;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "ctxt ", 5)) {--------------------------------------------ctxt鏄墍鏈塁PU鐨勮繘绋嬪垏鎹㈡鏁般€?            /* Read number of context switches */
            sscanf(line + 5, "%llu", &st_pcsw->context_switch);
        }

        else if (!strncmp(line, "processes ", 10)) {---------------------------------鏄暣涓郴缁熷垱寤鸿繘绋嬬殑娆℃暟锛宼otal_forks锛?            /* Read number of processes created since system boot */
            sscanf(line + 10, "%lu", &st_pcsw->processes);
        }
    }

    fclose(fp);
}

 

3.3 swap淇℃伅

swap_act璇诲彇/proc/vmstat鑺傜偣锛岃В鏋愬叾涓殑pswpin鍜宲swpout涓や釜淇℃伅銆?/p>

鎶€鏈垎浜浘鐗? src=

 

void read_vmstat_swap(struct stats_swap *st_swap)
{
    FILE *fp;
    char line[128];

    if ((fp = fopen(VMSTAT, "r")) == NULL)
        return;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "pswpin ", 7)) {-------------------------------瀵瑰簲PSWPIN锛岃〃绀轰粠swap鍒嗗尯璇诲叆椤甸潰銆?            /* Read number of swap pages brought in */
            sscanf(line + 7, "%lu", &st_swap->pswpin);
        }
        else if (!strncmp(line, "pswpout ", 8)) {-------------------------瀵瑰簲PSWPOUT锛岃〃绀哄皢page鍐欏叆鍒皊wap鍒嗗尯銆?            /* Read number of swap pages brought out */
            sscanf(line + 8, "%lu", &st_swap->pswpout);
        }
    }

    fclose(fp);
}

 

3.4 椤甸潰浜ゆ崲缁熻淇℃伅

paging_act缁熻/proc/vmstat涓〉闈氦鎹㈢粺璁′俊鎭€?/p>

Paging鍖呮嫭鍥涘紶鍥炬爣锛屽垎鍒粺璁?proc/vmstat涓殑PGPGIN銆丳GPGOUT銆丳GFAULT銆丳GMAJFAULT绛夌瓑淇℃伅銆?/p>

鎶€鏈垎浜浘鐗? src=

 

void read_vmstat_paging(struct stats_paging *st_paging)
{
    FILE *fp;
    char line[128];
    unsigned long pgtmp;

    if ((fp = fopen(VMSTAT, "r")) == NULL)
        return;

    st_paging->pgsteal = 0;
    st_paging->pgscan_kswapd = st_paging->pgscan_direct = 0;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "pgpgin ", 7)) {------------------------------------------------pgpgin鍜宲gpgout鍒嗗埆瀵瑰簲PGPGIN鍜孭GPGOUT锛屽湪submit_io()涓洿鏂般€傛寚鍐呭瓨鍜屽潡璁惧蹇楀潥page鏁扮洰锛岃繖閲岀殑page鎸囩殑鏄痙isk sector銆俻gpgin琛ㄧず浠庡潡璁惧璇诲叆锛宲gpgout鍐欏叆鍒拌法璁惧銆?            /* Read number of pages the system paged in */
            sscanf(line + 7, "%lu", &st_paging->pgpgin);
        }
        else if (!strncmp(line, "pgpgout ", 8)) {
            /* Read number of pages the system paged out */
            sscanf(line + 8, "%lu", &st_paging->pgpgout);
        }
        else if (!strncmp(line, "pgfault ", 8)) {-------------------------------------------鍦╲m_event_item[]涓搴擯GFAULT锛屽湪handle_mm_fault()涓洿鏂般€傜粺璁′骇鐢焢age fault鐨勪俊鎭€?            /* Read number of faults (major+minor) made by the system */
            sscanf(line + 8, "%lu", &st_paging->pgfault);
        }
        else if (!strncmp(line, "pgmajfault ", 11)) {---------------------------------------瀵瑰簲PGMAJFAULT锛岃〃绀轰粠纾佺洏鑰屼笉鏄粠鍐呭瓨涓幏鍙栨暟鎹€?            /* Read number of faults (major only) made by the system */
            sscanf(line + 11, "%lu", &st_paging->pgmajfault);
        }
        else if (!strncmp(line, "pgfree ", 7)) {--------------------------------------------瀵瑰簲PGFREE锛岀粺璁¢噴鏀剧殑椤甸潰娆℃暟銆?            /* Read number of pages freed by the system */
            sscanf(line + 7, "%lu", &st_paging->pgfree);
        }
        else if (!strncmp(line, "pgsteal_", 8)) {-------------------------------------------缁熻PGSTEAL_KSWAPD鍜孭GSTEAL_DIRECT淇℃伅锛岃〃绀虹郴缁熷洖鏀剁殑kswapd鍜岀洿鎺ュ洖鏀剁殑椤甸潰鏁扮洰銆?            /* Read number of pages stolen by the system */
            sscanf(strchr(line, 鈥?/span> 鈥?/span>), "%lu", &pgtmp);
            st_paging->pgsteal += pgtmp;
        }
        else if (!strncmp(line, "pgscan_kswapd", 13)) {--------------------------------------缁熻PGSCAN_KSWAPD淇℃伅锛岃〃绀轰粠绯荤粺鍚姩鍒扮幇鍦╧swapd鍚庡彴杩涚▼鎵弿鐨勯〉闈㈡暟銆?            /* Read number of pages scanned by the kswapd daemon */
            sscanf(strchr(line, 鈥?/span> 鈥?/span>), "%lu", &pgtmp);
            st_paging->pgscan_kswapd += pgtmp;
        }
        else if (!strncmp(line, "pgscan_direct", 13)) {--------------------------------------缁熻PGSCAN_DIRECT鍜孭GSCAN_DIRECT_THROTTLE锛岀粺璁′笘鐣屽洖鏀堕〉闈㈡暟銆?            /* Read number of pages scanned directly */
            sscanf(strchr(line, 鈥?/span> 鈥?/span>), "%lu", &pgtmp);
            st_paging->pgscan_direct += pgtmp;
        }
    }

    fclose(fp);
}

 

3.5 IO缁熻淇℃伅

io_act浠?proc/diskstats璇诲彇IO缁熻淇℃伅銆?/p>

鎶€鏈垎浜浘鐗? src=

涓婇潰鐨則ps銆乺tps銆亀tps銆乥read/s銆乥wrtn/s鍒嗗埆瀵瑰簲閲囨牱缁撴灉dk_drive銆乨k_drive_rio銆乨k_drive_wio銆乨k_drive_rblk銆乨k_drive_wblk銆?/p>

鍏朵腑tps涓簉tps鍜寃tps涔嬪拰锛岃〃绀哄畬鎴愯鍐欐鏁帮紱bread/s鍜宐wrtn/s琛ㄧず璇汇€佸啓鎵囧尯鏁扮洰銆?/p>

杩?涓弬鏁伴兘鏉ユ簮浜?a href="#diskstats_show" target="_blank">diskstats_show()鍑芥暟銆?/p>

void read_diskstats_io(struct stats_io *st_io)
{
    FILE *fp;
    char line[256];
    char dev_name[MAX_NAME_LEN];
    unsigned int major, minor;
    unsigned long rd_ios, wr_ios, rd_sec, wr_sec;

    if ((fp = fopen(DISKSTATS, "r")) == NULL)
        return;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (sscanf(line, "%u %u %s %lu %*u %lu %*u %lu %*u %lu",-----------scanf鐨?u涔嬮棿鐨勬槦鍙凤紝琛ㄧず璺宠繃姝よ緭鍏ャ€?               &major, &minor, dev_name,
               &rd_ios, &rd_sec, &wr_ios, &wr_sec) == 7) {

            if (is_device(dev_name, IGNORE_VIRTUAL_DEVICES)) {--------------铏氭嫙璁惧娌℃湁/sys/block/<device>/device锛屾鐗规€х敤浠ュ垽鏂璬ev_name瀵瑰簲鐨勮澶囨槸鍚︽槸鐪熷疄鐨勫潡璁惧銆?                st_io->dk_drive      += (unsigned long long) rd_ios + (unsigned long long) wr_ios;
                st_io->dk_drive_rio  += rd_ios;
                st_io->dk_drive_rblk += rd_sec;
                st_io->dk_drive_wio  += wr_ios;
                st_io->dk_drive_wblk += wr_sec;
            }
        }
    }
    fclose(fp);
}

/proc/diskstats鏉ユ簮浜庡唴鏍哥殑diskstats_show()

static int diskstats_show(struct seq_file *seqf, void *v)
{
    struct gendisk *gp = v;
    struct disk_part_iter piter;
    struct hd_struct *hd;
    char buf[BDEVNAME_SIZE];
    int cpu;

    /*
    if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
        seq_puts(seqf,    "major minor name"
                "     rio rmerge rsect ruse wio wmerge "
                "wsect wuse running use aveq"
                "

");
    */

    disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
    while ((hd = disk_part_iter_next(&piter))) {
        cpu = part_stat_lock();
        part_round_stats(cpu, hd);
        part_stat_unlock();
        seq_printf(seqf, "%4d %7d %s %lu %lu %lu "
               "%u %lu %lu %lu %u %u %u %u
",
               MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
               disk_name(gp, hd->partno, buf),
               part_stat_read(hd, ios[READ]),-------------------------------------------------鎴愬姛瀹屾垚璇荤殑娆℃暟銆?/span>
               part_stat_read(hd, merges[READ]),----------------------------------------------鍚堝苟璇绘鏁帮紝涓轰簡鏁堢巼鍙兘浼氬悎骞剁浉閭荤殑璇诲拰鍐欍€?/span>
               part_stat_read(hd, sectors[READ]),---------------------------------------------璇绘墖鍖虹殑娆℃暟銆?/span>
               jiffies_to_msecs(part_stat_read(hd, ticks[READ])),-----------------------------璇昏姳鐨勬椂闂达紝杩欓噷鏄墍鏈夎鎿嶄綔鎵€鑺辫垂鐨勬绉掓暟銆?/span>
               part_stat_read(hd, ios[WRITE]),------------------------------------------------鎴愬姛瀹屾垚鍐欑殑娆℃暟銆?/span>
               part_stat_read(hd, merges[WRITE]),---------------------------------------------鍚堝苟鍐欐鏁般€?/span>
               part_stat_read(hd, sectors[WRITE]),--------------------------------------------鍐欐墖鍖烘鏁般€?/span>
               jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])),----------------------------鍐欒姳鐨勬椂闂淬€?/span>
               part_in_flight(hd),
               jiffies_to_msecs(part_stat_read(hd, io_ticks)),
               jiffies_to_msecs(part_stat_read(hd, time_in_queue))
            );
    }
    disk_part_iter_exit(&piter);

    return 0;
}

 

 

3.6 鍐呭瓨鍙妔wap缁熻淇℃伅

memory_act缁熻/proc/meminfo鐨勫唴瀛樺拰swap淇℃伅銆?/p>

鎶€鏈垎浜浘鐗? src=

浠庡浘琛ㄤ腑鐨勫悕绉板ぇ姒傚氨鑳芥壘鍒板搴旂殑/proc/meminfo涓殑瀛楃椤广€?/p>

鍏朵腑%memused鍜?commit鏌ョ湅stub_print_memory_stats()锛?memused涓?MemTotal-MemFree)/MemTotal銆?/p>

void read_meminfo(struct stats_memory *st_memory)
{
    FILE *fp;
    char line[128];

    if ((fp = fopen(MEMINFO, "r")) == NULL)
        return;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "MemTotal:", 9)) {
            /* Read the total amount of memory in kB */
            sscanf(line + 9, "%lu", &st_memory->tlmkb);
        }
        else if (!strncmp(line, "MemFree:", 8)) {
            /* Read the amount of free memory in kB */
            sscanf(line + 8, "%lu", &st_memory->frmkb);
        }
        else if (!strncmp(line, "MemAvailable:", 13)) {
            /* Read the amount of available memory in kB */
            sscanf(line + 13, "%lu", &st_memory->availablekb);
        }
        else if (!strncmp(line, "Buffers:", 8)) {
            /* Read the amount of buffered memory in kB */
            sscanf(line + 8, "%lu", &st_memory->bufkb);
        }
        else if (!strncmp(line, "Cached:", 7)) {
            /* Read the amount of cached memory in kB */
            sscanf(line + 7, "%lu", &st_memory->camkb);
        }
        else if (!strncmp(line, "SwapCached:", 11)) {
            /* Read the amount of cached swap in kB */
            sscanf(line + 11, "%lu", &st_memory->caskb);
        }
        else if (!strncmp(line, "Active:", 7)) {
            /* Read the amount of active memory in kB */
            sscanf(line + 7, "%lu", &st_memory->activekb);
        }
        else if (!strncmp(line, "Inactive:", 9)) {
            /* Read the amount of inactive memory in kB */
            sscanf(line + 9, "%lu", &st_memory->inactkb);
        }
        else if (!strncmp(line, "SwapTotal:", 10)) {
            /* Read the total amount of swap memory in kB */
            sscanf(line + 10, "%lu", &st_memory->tlskb);
        }
        else if (!strncmp(line, "SwapFree:", 9)) {
            /* Read the amount of free swap memory in kB */
            sscanf(line + 9, "%lu", &st_memory->frskb);
        }
        else if (!strncmp(line, "Dirty:", 6)) {
            /* Read the amount of dirty memory in kB */
            sscanf(line + 6, "%lu", &st_memory->dirtykb);
        }
        else if (!strncmp(line, "Committed_AS:", 13)) {
            /* Read the amount of commited memory in kB */
            sscanf(line + 13, "%lu", &st_memory->comkb);
        }
        else if (!strncmp(line, "AnonPages:", 10)) {
            /* Read the amount of pages mapped into userspace page tables in kB */
            sscanf(line + 10, "%lu", &st_memory->anonpgkb);
        }
        else if (!strncmp(line, "Slab:", 5)) {
            /* Read the amount of in-kernel data structures cache in kB */
            sscanf(line + 5, "%lu", &st_memory->slabkb);
        }
        else if (!strncmp(line, "KernelStack:", 12)) {
            /* Read the kernel stack utilization in kB */
            sscanf(line + 12, "%lu", &st_memory->kstackkb);
        }
        else if (!strncmp(line, "PageTables:", 11)) {
            /* Read the amount of memory dedicated to the lowest level of page tables in kB */
            sscanf(line + 11, "%lu", &st_memory->pgtblkb);
        }
        else if (!strncmp(line, "VmallocUsed:", 12)) {
            /* Read the amount of vmalloc area which is used in kB */
            sscanf(line + 12, "%lu", &st_memory->vmusedkb);
        }
    }

    fclose(fp);
}

涓嬪浘鐨刱bswpfree銆乲bswpused銆乲bswpcad閫氳繃瀛楅潰鎰忔€濆嵆鍙煡鍏跺搴旂殑meminfo椤逛负SwapFree銆丼wapTotal-SwapFree銆丼wapCached銆?/p>

鎶€鏈垎浜浘鐗? src=

 

3.7 涓柇缁熻淇℃伅

涓柇浣犵粺璁′俊鎭悓鏍锋潵鑷?proc/stat锛岃В鏋恑ntr瀛楁銆傜涓€涓槸鍚姩浠ユ潵鎵€鏈変腑鏂Е鍙戞鏁帮紝鍚庨潰鏄崟涓腑鏂Е鍙戞鏁般€?/p>

鐢熸垚鐨勮〃鏍间腑鍖呭惈浜嗗搴旂殑鍥炬爣sum锛屼互鍙婃瘡涓腑鏂粺璁′俊鎭€?/p>

鎶€鏈垎浜浘鐗? src=

void read_stat_irq(struct stats_irq *st_irq, int nbr)
{
    FILE *fp;
    struct stats_irq *st_irq_i;
    char line[8192];
    int i, pos;

    if ((fp = fopen(STAT, "r")) == NULL)
        return;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "intr ", 5)) {-------------------------------------------------/proc/stat鐨刬ntr瀛楁銆?            /* Read total number of interrupts received since system boot */
            sscanf(line + 5, "%llu", &st_irq->irq_nr);
            pos = strcspn(line + 5, " ") + 5;

            for (i = 1; i < nbr; i++) {
                st_irq_i = st_irq + i;
                sscanf(line + pos, " %llu", &st_irq_i->irq_nr);
                pos += strcspn(line + pos + 1, " ") + 1;
            }
        }
    }

    fclose(fp);
}

 

 

3.8 

ktables_act

#define FDENTRY_STATE    "/proc/sys/fs/dentry-state"
#define FFILE_NR    "/proc/sys/fs/file-nr"
#define FINODE_STATE    "/proc/sys/fs/inode-state"
#define PTY_NR        "/proc/sys/kernel/pty/nr"

void read_kernel_tables(struct stats_ktables *st_ktables)
{
    FILE *fp;
    unsigned int parm;
    int rc = 0;

    /* Open /proc/sys/fs/dentry-state file */
    if ((fp = fopen(FDENTRY_STATE, "r")) != NULL) {
        rc = fscanf(fp, "%*d %u",
                &st_ktables->dentry_stat);
        fclose(fp);
        if (rc == 0) {
            st_ktables->dentry_stat = 0;
        }
    }

    /* Open /proc/sys/fs/file-nr file */
    if ((fp = fopen(FFILE_NR, "r")) != NULL) {
        rc = fscanf(fp, "%u %u",
                &st_ktables->file_used, &parm);
        fclose(fp);
        /*
         * The number of used handles is the number of allocated ones
         * minus the number of free ones.
         */
        if (rc == 2) {
            st_ktables->file_used -= parm;
        }
        else {
            st_ktables->file_used = 0;
        }
    }

    /* Open /proc/sys/fs/inode-state file */
    if ((fp = fopen(FINODE_STATE, "r")) != NULL) {
        rc = fscanf(fp, "%u %u",
                &st_ktables->inode_used, &parm);
        fclose(fp);
        /*
         * The number of inuse inodes is the number of allocated ones
         * minus the number of free ones.
         */
        if (rc == 2) {
            st_ktables->inode_used -= parm;
        }
        else {
            st_ktables->inode_used = 0;
        }
    }

    /* Open /proc/sys/kernel/pty/nr file */
    if ((fp = fopen(PTY_NR, "r")) != NULL) {
        rc = fscanf(fp, "%u",
                &st_ktables->pty_nr);
        fclose(fp);
        if (rc == 0) {
            st_ktables->pty_nr = 0;
        }
    }
}

 

3.9 load鍜宷ueue缁熻淇℃伅

queue_act浠?proc/loadavg鑾峰彇load淇℃伅锛屼粠/proc/stat鑾峰彇queue缁熻淇℃伅銆?/p>

鎶€鏈垎浜浘鐗? src=

ldavg-1銆乴davg-5銆乴davg-15鍒嗗埆琛ㄧず1銆?銆?5鍒嗛挓鍐呰繘绋嬪绔嬩腑骞冲潎杩涚▼鏁扮洰锛屽寘鎷鍦ㄨ繍琛岀殑杩涚▼鍜屽噯澶囧ソ绛夊緟杩愯鐨勮繘绋嬫暟鐩€?/p>

runq-sz琛ㄧず杩愯闃熷垪澶у皬锛宲list-sz琛ㄧず鎵€鏈夎繘绋嬫€绘暟nr_threads銆?/p>

runq-sz澶ц〃绀虹瓑寰呰繍琛岀殑杩涚▼鏁扮洰杈冨銆?/p>

blocked琛ㄧず澶勪簬iowait鐘舵€佺殑杩涚▼鏁扮洰銆?/p>

void read_loadavg(struct stats_queue *st_queue)
{
    FILE *fp;
    char line[8192];
    int load_tmp[3];
    int rc;

    if ((fp = fopen(LOADAVG, "r")) == NULL)
        return;

    /* Read load averages and queue length */
    rc = fscanf(fp, "%d.%u %d.%u %d.%u %lu/%u %*d
",------------------------------鍙互鐪嬪嚭浠?proc/loadavg涓幏鍙栦俊鎭紝鐒跺悗鏈€鍚庝竴涓暟鎹拷鐣ャ€?            &load_tmp[0], &st_queue->load_avg_1,
            &load_tmp[1], &st_queue->load_avg_5,
            &load_tmp[2], &st_queue->load_avg_15,
            &st_queue->nr_running,
            &st_queue->nr_threads);

    fclose(fp);

    if (rc < 8)
        return;

    st_queue->load_avg_1  += load_tmp[0] * 100;
    st_queue->load_avg_5  += load_tmp[1] * 100;
    st_queue->load_avg_15 += load_tmp[2] * 100;

    if (st_queue->nr_running) {
        /* Do not take current process into account */
        st_queue->nr_running--;
    }

    /* Read nr of tasks blocked from /proc/stat */
    if ((fp = fopen(STAT, "r")) == NULL)
        return;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "procs_blocked ", 14)) {--------------------------------浠?proc/stat涓幏鍙杙rocs_blocked椤癸紝琛ㄧず澶勪簬iowait鐘舵€佺殑杩涚▼鏁扮洰銆?            /* Read number of processes blocked */
            sscanf(line + 14, "%lu", &st_queue->procs_blocked);
            break;
        }
    }

    fclose(fp);
}

 

 

3.10 鍧楄澶囩粺璁′俊鎭?/h2>

disk_act鍜宨o_act涓€鏍锋槸浠?proc/diskstats涓幏鍙栫粺璁′俊鎭紝

鎶€鏈垎浜浘鐗? src=

鍥炬爣涓殑鍥涢」tps琛ㄧず鍧楄澶囪鍐欐鏁伴鐜囷紱rkB/s鍜寃kB/s琛ㄧず鍧楄澶囪鍐欓€熺巼锛沘wait琛ㄧず涓€娆¤鎴栧啓鑰楁椂銆?/p>

tps鍊煎ぇ璇存槑姝ゆ鏃堕棿璇诲啓杈冮绻併€?/p>

await鍊煎ぇ琛ㄧず褰撳墠璇诲啓鑰楁椂杈冨ぇ锛屽彲鑳藉瓨鍦ㄩ棶棰樸€?/p>

__print_funct_t print_disk_stats(struct activity *a, int prev, int curr,
                 unsigned long long itv)
{
...

    for (i = 0; i < a->nr; i++) {
...
        printf("%-11s", timestamp[curr]);

        cprintf_in(IS_STR, " %9s", dev_name, 0);-----------------------------sdc鏄澶囧綋鍓嶇粺璁′俊鎭紝sdp鏄澶囧墠涓€娆$粺璁′俊鎭紝itv鏄袱娆℃椂闂撮棿闅斻€?/span>
        cprintf_f(NO_UNIT, 1, 9, 2,
              S_VALUE(sdp->nr_ios, sdc->nr_ios,  itv));----------------------瀵瑰簲鍥炬爣涓殑tps锛宯r_ios琛ㄧず鍧楄澶囪鍐欐鏁颁箣鍜屻€倀ps琛ㄧず鍧楄澶囪鍐欓鐜囥€?/span>
        cprintf_f(unit, 2, 9, 2,
              S_VALUE(sdp->rd_sect, sdc->rd_sect, itv) / 2,
              S_VALUE(sdp->wr_sect, sdc->wr_sect, itv) / 2);-----------------瀵瑰簲鍥炬爣鐨剅kB/s鍜寃kB/s锛屽悓鏍穜d_sect鍜寃r_sect鍙互璁$畻鍑轰互KB涓哄崟浣嶇殑璇诲啓閫熺巼銆?        /* See iostat for explanations */
        cprintf_f(unit, 1, 9, 2,
              xds.arqsz / 2);
        cprintf_f(NO_UNIT, 3, 9, 2,
              S_VALUE(sdp->rq_ticks, sdc->rq_ticks, itv) / 1000.0,
              xds.await,------------------------------------------------------瀵瑰簲await锛屽湪compute_ext_disk_stats()涓绠椼€傝〃绀烘瘡娆¤鍐欏钩鍧囪€楁椂銆?/span>
              xds.svctm);
        cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2,
               xds.util / 10.0);
        printf("
");
    }
}

 

 

3.11 CPU棰戠巼

pwr_cpufreq_act浠?proc/cpuinfo涓幏鍙栭鐜囦俊鎭紝CPU Frequency all鏄剧ず鐨勬槸鎵€鏈塁PU棰戠巼鐨勫钩鍧囧€笺€?/p>

鎶€鏈垎浜浘鐗? src=

浠庝笅闈唬鐮佸垎鏋愬彲鐭ワ紝浠庡叧閿瘝processor涓幏鍙朇PU鍙凤紝浠巆pu MHz鑾峰彇銆?/p>

void read_cpuinfo(struct stats_pwr_cpufreq *st_pwr_cpufreq, int nbr)
{
    FILE *fp;
    struct stats_pwr_cpufreq *st_pwr_cpufreq_i;
    char line[1024];
    int nr = 0;
    unsigned int proc_nb = 0, ifreq, dfreq;

    if ((fp = fopen(CPUINFO, "r")) == NULL)
        return;

    st_pwr_cpufreq->cpufreq = 0;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "processor	", 10)) {
            sscanf(strchr(line, 鈥?/span>:鈥?/span>) + 1, "%u", &proc_nb);
        }
        /* Entry in /proc/cpuinfo is different between Intel and Power architectures */
        else if (!strncmp(line, "cpu MHz	", 8) ||
             !strncmp(line, "clock	", 6)) {
            sscanf(strchr(line, 鈥?/span>:鈥?/span>) + 1, "%u.%u", &ifreq, &dfreq);

            if (proc_nb < (nbr - 1)) {
                st_pwr_cpufreq_i = st_pwr_cpufreq + proc_nb + 1;
                st_pwr_cpufreq_i->cpufreq = ifreq * 100 + dfreq / 10;

                st_pwr_cpufreq->cpufreq += st_pwr_cpufreq_i->cpufreq;
                nr++;
            }
            else if (!proc_nb && (nbr == 1)) {
                st_pwr_cpufreq->cpufreq = ifreq * 100 + dfreq / 10;
            }
        }
    }

    fclose(fp);
...
}

 

3.12 缁熻涓插彛绾夸俊鎭?/h2>

serial_act浠?proc/tty/driver/serial鑾峰彇涓插彛绾跨粺璁′俊鎭€?/p>

void read_tty_driver_serial(struct stats_serial *st_serial, int nbr)
{
    FILE *fp;
    struct stats_serial *st_serial_i;
    int sl = 0;
    char line[256];
    char *p;

    if ((fp = fopen(SERIAL, "r")) == NULL)
        return;

    while ((fgets(line, sizeof(line), fp) != NULL) && (sl < nbr)) {

        if ((p = strstr(line, "tx:")) != NULL) {
            st_serial_i = st_serial + sl;
            sscanf(line, "%u", &st_serial_i->line);
            /*
             * A value of 0 means an unused structure.
             * So increment it to make sure it is not zero.
             */
            (st_serial_i->line)++;
            /*
             * Read the number of chars transmitted and received by
             * current serial line.
             */
            sscanf(p + 3, "%u", &st_serial_i->tx);
            if ((p = strstr(line, "rx:")) != NULL) {
                sscanf(p + 3, "%u", &st_serial_i->rx);
            }
            if ((p = strstr(line, "fe:")) != NULL) {
                sscanf(p + 3, "%u", &st_serial_i->frame);
            }
            if ((p = strstr(line, "pe:")) != NULL) {
                sscanf(p + 3, "%u", &st_serial_i->parity);
            }
            if ((p = strstr(line, "brk:")) != NULL) {
                sscanf(p + 4, "%u", &st_serial_i->brk);
            }
            if ((p = strstr(line, "oe:")) != NULL) {
                sscanf(p + 3, "%u", &st_serial_i->overrun);
            }

            sl++;
        }
    }

    fclose(fp);
}

 

3.13 鑾峰彇缃戝彛缁熻淇℃伅

net_dev_act浠?proc/net/dev涓幏鍙栫綉鍙g殑缁熻淇℃伅锛屼粠/sys/class/net/xxx/duplex鍜?sys/class/net/xxx/speed涓幏鍙栧弻宸ュ拰閫熷害淇℃伅銆?/p>

鎶€鏈垎浜浘鐗? src=

浠庝笅闈㈠叧浜?proc/net/dev瑙i噴鍙煡锛宺xpck瀵瑰簲rx_packets锛宺xpck/s灏辨槸姣忕鎺ユ敹packet鏁帮紱txpck/s灏辨槸姣忎釜鍙戦€乸acket鏁扮洰銆?/p>

rxkB/s鏄瘡绉掓帴鍙楀灏慿瀛楄妭锛泃xkB/s鏄瘡绉掑彂閫佸灏慿瀛楄妭銆?/p>

rxmcst/s琛ㄧず姣忕鎺ユ敹鍒扮殑multicast packet鏁扮洰銆?/p>

 

%ifutil璺熸瘡绉掑彂閫佹帴鏀剁殑KB鏁版湁鍏筹紝杩樿窡鍏ㄥ弻宸ワ紝鍗曞弻宸ユ湁鍏筹紝浠ュ強璁惧鐨勯€熷害鏈夊叧锛涜〃绀虹綉缁滆澶囦娇鐢ㄧ巼銆?/p>

鍙屽伐淇℃伅浠?sys/class/net/%s/duplex鑾峰彇锛岄€熷害淇℃伅浠庝粠/sys/class/net/%s/speed鑾峰彇銆?/p>

浠庢鍥剧湅锛岀綉缁滅殑浣跨敤鐜囧苟涓嶉珮锛岃鏄庣綉缁滀笉棰戠箒銆?/p>

 

int read_net_dev(struct stats_net_dev *st_net_dev, int nbr)
{
    FILE *fp;
    struct stats_net_dev *st_net_dev_i;
    char line[256];
    char iface[MAX_IFACE_LEN];
    int dev = 0;
    int pos;

    if ((fp = fopen(NET_DEV, "r")) == NULL)----------------------------------------------------------------浠?proc/net/dev鑾峰彇淇℃伅銆?        return 0;

    while ((fgets(line, sizeof(line), fp) != NULL) && (dev < nbr)) {

        pos = strcspn(line, ":");
        if (pos < strlen(line)) {
            st_net_dev_i = st_net_dev + dev;
            strncpy(iface, line, MINIMUM(pos, MAX_IFACE_LEN - 1));
            iface[MINIMUM(pos, MAX_IFACE_LEN - 1)] = 鈥?/span>鈥?/span>;
            sscanf(iface, "%s", st_net_dev_i->interface); /* Skip heading spaces */
            sscanf(line + pos + 1, "%llu %llu %*u %*u %*u %*u %llu %llu %llu %llu "
                   "%*u %*u %*u %*u %*u %llu",
                   &st_net_dev_i->rx_bytes,
                   &st_net_dev_i->rx_packets,
                   &st_net_dev_i->rx_compressed,
                   &st_net_dev_i->multicast,
                   &st_net_dev_i->tx_bytes,
                   &st_net_dev_i->tx_packets,
                   &st_net_dev_i->tx_compressed);
            dev++;
        }
    }

    fclose(fp);

    return dev;
}

void read_if_info(struct stats_net_dev *st_net_dev, int nbr)
{
    FILE *fp;
    struct stats_net_dev *st_net_dev_i;
    char filename[128], duplex[32];
    int dev, n;

    for (dev = 0; dev < nbr; dev++) {

        st_net_dev_i = st_net_dev + dev;

        /* Read speed info */
        sprintf(filename, IF_DUPLEX, st_net_dev_i->interface);----------------------------------------------浠?sys/class/net/%s/duplex鑾峰彇淇℃伅銆?
        if ((fp = fopen(filename, "r")) == NULL)
            /* Cannot read NIC duplex */
            continue;

        n = fscanf(fp, "%31s", duplex);

        fclose(fp);

        if (n != 1)
            /* Cannot read NIC duplex */
            continue;

        if (!strcmp(duplex, K_DUPLEX_FULL)) {
            st_net_dev_i->duplex = C_DUPLEX_FULL;
        }
        else if (!strcmp(duplex, K_DUPLEX_HALF)) {
            st_net_dev_i->duplex = C_DUPLEX_HALF;
        }
        else
            continue;

        /* Read speed info */
        sprintf(filename, IF_SPEED, st_net_dev_i->interface);--------------------------------------------------浠?sys/class/net/%s/speed鑾峰彇淇℃伅銆?
        if ((fp = fopen(filename, "r")) == NULL)
            /* Cannot read NIC speed */
            continue;

        n = fscanf(fp, "%u", &st_net_dev_i->speed);

        fclose(fp);

        if (n != 1) {
            st_net_dev_i->speed = 0;
        }
    }
}

 

3.13.1 /proc/net/dev

/proc/net/dev鑺傜偣鍦╠ev_proc_net_init()涓垱寤猴紝瀵瑰簲鐨刦ops鏄痙ev_seq_fops銆?/p>

杩欓噷閲嶇偣鐪嬩竴涓媎ev_seq_show()锛?/p>

static int dev_seq_show(struct seq_file *seq, void *v)
{
    if (v == SEQ_START_TOKEN)
        seq_puts(seq, "Inter-|   Receive                            "
                  "                    |  Transmit
"
                  " face |bytes    packets errs drop fifo frame "
                  "compressed multicast|bytes    packets errs "
                  "drop fifo colls carrier compressed
");
    else
        dev_seq_printf_stats(seq, v);
    return 0;
}

static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
{
    struct rtnl_link_stats64 temp;
    const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);

    seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
           "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu
",
           dev->name, stats->rx_bytes, stats->rx_packets,
           stats->rx_errors,
           stats->rx_dropped + stats->rx_missed_errors,
           stats->rx_fifo_errors,
           stats->rx_length_errors + stats->rx_over_errors +
            stats->rx_crc_errors + stats->rx_frame_errors,
           stats->rx_compressed, stats->multicast,
           stats->tx_bytes, stats->tx_packets,
           stats->tx_errors, stats->tx_dropped,
           stats->tx_fifo_errors, stats->collisions,
           stats->tx_carrier_errors +
            stats->tx_aborted_errors +
            stats->tx_window_errors +
            stats->tx_heartbeat_errors,
           stats->tx_compressed);
}

 

 

 

3.14 缃戠粶閿欒缁熻淇℃伅

net_edev_act浠?proc/net/dev缃戠粶閿欒缁熻淇℃伅銆?/p>

鎶€鏈垎浜浘鐗? src=

缃戠粶閿欒淇℃伅鏁版嵁鏉ユ簮鍜岀綉缁滀俊鎭竴鏍凤紝閮芥潵鑷簬/proc/net/dev銆?/p>

鍏抽敭缁撴瀯浣撲篃鏄痵truct rtnl_link_stats64銆?/p>

void read_net_edev(struct stats_net_edev *st_net_edev, int nbr)
{
    FILE *fp;
    struct stats_net_edev *st_net_edev_i;
    static char line[256];
    char iface[MAX_IFACE_LEN];
    int dev = 0;
    int pos;

    if ((fp = fopen(NET_DEV, "r")) == NULL)
        return;

    while ((fgets(line, sizeof(line), fp) != NULL) && (dev < nbr)) {

        pos = strcspn(line, ":");
        if (pos < strlen(line)) {
            st_net_edev_i = st_net_edev + dev;
            strncpy(iface, line, MINIMUM(pos, MAX_IFACE_LEN - 1));
            iface[MINIMUM(pos, MAX_IFACE_LEN - 1)] = 鈥?/span>鈥?/span>;
            sscanf(iface, "%s", st_net_edev_i->interface); /* Skip heading spaces */
            sscanf(line + pos + 1, "%*u %*u %llu %llu %llu %llu %*u %*u %*u %*u "
                   "%llu %llu %llu %llu %llu",
                   &st_net_edev_i->rx_errors,------------------瀵瑰簲rx_erros锛岃〃绀烘帴鍙楀埌鐨刡ad packet銆?                   &st_net_edev_i->rx_dropped,-----------------瀵瑰簲rx_dropped+rx_missed_errors銆?                   &st_net_edev_i->rx_fifo_errors,-------------瀵瑰簲rx_fifo_errors銆?                   &st_net_edev_i->rx_frame_errors,------------瀵瑰簲rx_length_errors+rx_over_errros銆?                   &st_net_edev_i->tx_errors,
                   &st_net_edev_i->tx_dropped,
                   &st_net_edev_i->tx_fifo_errors,
                   &st_net_edev_i->collisions,-----------------瀵瑰簲collisions
                   &st_net_edev_i->tx_carrier_errors);---------鍖呮嫭tx_carrier_errors+tx_aborted_errors+tx_window_errors+tx_heartbeat_errors銆?/pre>
            dev++;
        }
    }

    fclose(fp);
}

鑾峰彇缃戠粶璁惧缁熻淇℃伅鐨勬牳蹇冩暟鎹粨鏋勫紡struct rtnl_link_stat64銆?/p>

struct rtnl_link_stats64 {
    __u64    rx_packets;        /* total packets received    */
    __u64    tx_packets;        /* total packets transmitted    */
    __u64    rx_bytes;        /* total bytes received     */
    __u64    tx_bytes;        /* total bytes transmitted    */
    __u64    rx_errors;        /* bad packets received        */
    __u64    tx_errors;        /* packet transmit problems    */
    __u64    rx_dropped;        /* no space in linux buffers    */
    __u64    tx_dropped;        /* no space available in linux    */
    __u64    multicast;        /* multicast packets received    */
    __u64    collisions;

    /* detailed rx_errors: */
    __u64    rx_length_errors;
    __u64    rx_over_errors;        /* receiver ring buff overflow    */
    __u64    rx_crc_errors;        /* recved pkt with crc error    */
    __u64    rx_frame_errors;    /* recv鈥榙 frame alignment error */
    __u64    rx_fifo_errors;        /* recv鈥榬 fifo overrun        */
    __u64    rx_missed_errors;    /* receiver missed packet    */

    /* detailed tx_errors */
    __u64    tx_aborted_errors;
    __u64    tx_carrier_errors;
    __u64    tx_fifo_errors;
    __u64    tx_heartbeat_errors;
    __u64    tx_window_errors;

    /* for cslip etc */
    __u64    rx_compressed;
    __u64    tx_compressed;

    __u64    rx_nohandler;        /* dropped, no handler found    */
};

 

3.15 NFS瀹㈡埛绔粺璁′俊鎭?/h2>

net_nfs_act璇诲彇/proc/net/rpc/nfs鏉ュ垎鏋愪綔涓簄fs瀹㈡埛绔殑缁熻淇℃伅銆?/p>

void read_net_nfs(struct stats_net_nfs *st_net_nfs)
{
    FILE *fp;
    char line[256];
    unsigned int getattcnt = 0, accesscnt = 0, readcnt = 0, writecnt = 0;

    if ((fp = fopen(NET_RPC_NFS, "r")) == NULL)
        return;

    memset(st_net_nfs, 0, STATS_NET_NFS_SIZE);

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "rpc ", 4)) {
            sscanf(line + 4, "%u %u",
                   &st_net_nfs->nfs_rpccnt, &st_net_nfs->nfs_rpcretrans);
        }
        else if (!strncmp(line, "proc3 ", 6)) {
            sscanf(line + 6, "%*u %*u %u %*u %*u %u %*u %u %u",
                   &getattcnt, &accesscnt, &readcnt, &writecnt);

            st_net_nfs->nfs_getattcnt += getattcnt;
            st_net_nfs->nfs_accesscnt += accesscnt;
            st_net_nfs->nfs_readcnt   += readcnt;
            st_net_nfs->nfs_writecnt  += writecnt;
        }
        else if (!strncmp(line, "proc4 ", 6)) {
            sscanf(line + 6, "%*u %*u %u %u "
                   "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u %u",
                   &readcnt, &writecnt, &accesscnt, &getattcnt);

            st_net_nfs->nfs_getattcnt += getattcnt;
            st_net_nfs->nfs_accesscnt += accesscnt;
            st_net_nfs->nfs_readcnt   += readcnt;
            st_net_nfs->nfs_writecnt  += writecnt;
        }
    }

    fclose(fp);
}

 

3.16 NFS鏈嶅姟绔粺璁′俊鎭?/h2>

net_nfsd_act

#define NET_RPC_NFSD "/proc/net/rpc/nfsd"

void read_net_nfsd(struct stats_net_nfsd *st_net_nfsd)
{
    FILE *fp;
    char line[256];
    unsigned int getattcnt = 0, accesscnt = 0, readcnt = 0, writecnt = 0;

    if ((fp = fopen(NET_RPC_NFSD, "r")) == NULL)
        return;

    memset(st_net_nfsd, 0, STATS_NET_NFSD_SIZE);

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "rc ", 3)) {
            sscanf(line + 3, "%u %u",
                   &st_net_nfsd->nfsd_rchits, &st_net_nfsd->nfsd_rcmisses);
        }
        else if (!strncmp(line, "net ", 4)) {
            sscanf(line + 4, "%u %u %u",
                   &st_net_nfsd->nfsd_netcnt, &st_net_nfsd->nfsd_netudpcnt,
                   &st_net_nfsd->nfsd_nettcpcnt);
        }
        else if (!strncmp(line, "rpc ", 4)) {
            sscanf(line + 4, "%u %u",
                   &st_net_nfsd->nfsd_rpccnt, &st_net_nfsd->nfsd_rpcbad);
        }
        else if (!strncmp(line, "proc3 ", 6)) {
            sscanf(line + 6, "%*u %*u %u %*u %*u %u %*u %u %u",
                   &getattcnt, &accesscnt, &readcnt, &writecnt);

            st_net_nfsd->nfsd_getattcnt += getattcnt;
            st_net_nfsd->nfsd_accesscnt += accesscnt;
            st_net_nfsd->nfsd_readcnt   += readcnt;
            st_net_nfsd->nfsd_writecnt  += writecnt;

        }
        else if (!strncmp(line, "proc4ops ", 9)) {
            sscanf(line + 9, "%*u %*u %*u %*u %u "
                   "%*u %*u %*u %*u %*u %u "
                   "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u "
                   "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u",
                   &accesscnt, &getattcnt, &readcnt, &writecnt);

            st_net_nfsd->nfsd_getattcnt += getattcnt;
            st_net_nfsd->nfsd_accesscnt += accesscnt;
            st_net_nfsd->nfsd_readcnt   += readcnt;
            st_net_nfsd->nfsd_writecnt  += writecnt;
        }
    }

    fclose(fp);
}

 

3.17 缃戠粶socket缁熻淇℃伅

net_sock_act璇诲彇/proc/net/sockstat缁熻TCP鍜孶DP淇℃伅銆?/p>

void read_net_sock(struct stats_net_sock *st_net_sock)
{
    FILE *fp;
    char line[96];
    char *p;

    if ((fp = fopen(NET_SOCKSTAT, "r")) == NULL)
        return;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "sockets:", 8)) {
            /* Sockets */
            sscanf(line + 14, "%u", &st_net_sock->sock_inuse);
        }
        else if (!strncmp(line, "TCP:", 4)) {
            /* TCP sockets */
            sscanf(line + 11, "%u", &st_net_sock->tcp_inuse);
            if ((p = strstr(line, "tw")) != NULL) {
                sscanf(p + 2, "%u", &st_net_sock->tcp_tw);
            }
        }
        else if (!strncmp(line, "UDP:", 4)) {
            /* UDP sockets */
            sscanf(line + 11, "%u", &st_net_sock->udp_inuse);
        }
        else if (!strncmp(line, "RAW:", 4)) {
            /* RAW sockets */
            sscanf(line + 11, "%u", &st_net_sock->raw_inuse);
        }
        else if (!strncmp(line, "FRAG:", 5)) {
            /* FRAGments */
            sscanf(line + 12, "%u", &st_net_sock->frag_inuse);
        }
    }

    fclose(fp);
}

 

3.18 huge椤甸潰缁熻淇℃伅

huge_act閫氳繃璇诲彇/proc/meminfo鑾峰彇huge椤甸潰缁熻淇℃伅銆?/p>

void read_meminfo_huge(struct stats_huge *st_huge)
{
    FILE *fp;
    char line[128];
    unsigned long szhkb = 0;

    if ((fp = fopen(MEMINFO, "r")) == NULL)
        return;

    while (fgets(line, sizeof(line), fp) != NULL) {

        if (!strncmp(line, "HugePages_Total:", 16)) {
            /* Read the total number of huge pages */
            sscanf(line + 16, "%lu", &st_huge->tlhkb);
        }
        else if (!strncmp(line, "HugePages_Free:", 15)) {
            /* Read the number of free huge pages */
            sscanf(line + 15, "%lu", &st_huge->frhkb);
        }
        else if (!strncmp(line, "Hugepagesize:", 13)) {
            /* Read the default size of a huge page in kB */
            sscanf(line + 13, "%lu", &szhkb);
        }
    }

    fclose(fp);

    /* We want huge pages stats in kB and not expressed in a number of pages */
    st_huge->tlhkb *= szhkb;
    st_huge->frhkb *= szhkb;
}

 

3.19 softnet缁熻淇℃伅

softnet_act

#define NET_SOFTNET "/proc/net/softnet_stat"

void read_softnet(struct stats_softnet *st_softnet, int nbr)
{
    FILE *fp;
    struct stats_softnet *st_softnet_i;
    char line[1024];
    unsigned int proc_nb = 1;

    /* Open /proc/net/softnet_stat file */
    if ((fp = fopen(NET_SOFTNET, "r")) == NULL)
        return;

    /*
     * Init a structure that will contain the values for CPU "all".
     * CPU "all" doesn鈥榯 exist in /proc/net/softnet_stat file, so
     * we compute its values as the sum of the values of each CPU.
     */
    memset(st_softnet, 0, sizeof(struct stats_softnet));

    while ((fgets(line, sizeof(line), fp) != NULL) && (proc_nb < nbr)) {

        st_softnet_i = st_softnet + proc_nb++;
        sscanf(line, "%x %x %x %*x %*x %*x %*x %*x %*x %x %x",
               &st_softnet_i->processed,
               &st_softnet_i->dropped,
               &st_softnet_i->time_squeeze,
               &st_softnet_i->received_rps,
               &st_softnet_i->flow_limit);

        st_softnet->processed += st_softnet_i->processed;
        st_softnet->dropped += st_softnet_i->dropped;
        st_softnet->time_squeeze += st_softnet_i->time_squeeze;
        st_softnet->received_rps += st_softnet_i->received_rps;
        st_softnet->flow_limit += st_softnet_i->flow_limit;
    }

    fclose(fp);
}

 

 

 

以上是关于ksar銆乻ar鍙婄浉鍏冲唴鏍哥煡璇嗙偣瑙f瀽的主要内容,如果未能解决你的问题,请参考以下文章

redis鐨勫嚑涓煡璇嗙偣

鏁版嵁搴撶煡璇嗙偣

涓€闃舵鈥斺€旂煡璇嗙偣鏁村悎

鍏充簬姊害鐨勪竴浜涚煡璇嗙偣

鏂囨憳锛歮atplotlib缁樺浘鍙鍖栫煡璇嗙偣鏁寸悊

鏄撻敊鏄撴贩娣嗙煡璇嗙偣璁茶В绯诲垪锛堜節锛夆€斿缓绔嬪姩鎬佽鍒掓ā鍨嬬殑鍩烘湰姝ラ