c_cpp kstat_lookup和kstat_read用法的示例kstat片段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp kstat_lookup和kstat_read用法的示例kstat片段相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <kstat.h>


long getValue(kstat_t *ksp, char *name);

int main(void) {
    long value;
    kstat_ctl_t *kc;
    kstat_t *data;

    if ((kc = kstat_open()) == NULL) {
        fprintf(stderr, "Failed to open kstat!\n");
        exit(1);
    };

    if ((data = kstat_lookup(kc, "unix", 0, "dnlcstats")) == NULL) {
        fprintf(stderr, "Failed to kstat_lookup!\n");
        exit(1);
    } else {
        (void)kstat_read(kc, data, NULL);
        char *f[8] = {
            "double_enters", "enters",
            "hits", "misses", "negative_cache_hits", 
            "pick_free", "pick_heuristic", "pick_last",
        };
        for (int i = 0 ; i < 8 ; i++) {
            if ((value = getValue(data, f[i])) != -1) {
                printf("name: %s | value: %ld\n", f[i], value);
            }
        }
    }
    return 0;
}

long getValue(kstat_t *ksp, char *name) {
    int i;
    long value = -1;
    kstat_named_t *fields = KSTAT_NAMED_PTR(ksp);

    for (i = 0 ; i < ksp->ks_ndata ; i++, fields++) {
        if (!strcmp(name, fields->name)) {
            value = fields->value.ui64;
            break;
        }
    }
    return value;
}

以上是关于c_cpp kstat_lookup和kstat_read用法的示例kstat片段的主要内容,如果未能解决你的问题,请参考以下文章

sql SQL查询从kstat收集的数据

c_cpp _strlwr&_strupr

c_cpp 垫_

c_cpp -_-公元前

c_cpp uv__next_timeout.c

c_cpp uv__handle_unref.c