老白学编程

北城余情 提交于 2020-04-15 19:22:49

【推荐阅读】微服务还能火多久?>>>

Netdata - daemon

学习Netdata中的相关技术。

Version

netdata v1.14.0-71-ga5e952c

Option

定义opiton 。

struct option_def option_definitions[] = {
 265    // opt description                                    arg name       default value
 266    { 'c', "Configuration file to load.",                 "filename",    CONFIG_DIR "/" CONFIG_FILENAME},
 267    { 'D', "Do not fork. Run in the foreground.",         NULL,          "run in the background"},
 268    { 'd', "Fork. Run in the background.",                NULL,          "run in the background"},
 269    { 'h', "Display this help message.",                  NULL,          NULL},
 270    { 'P', "File to save a pid while running.",           "filename",    "do not save pid to a file"},
 271    { 'i', "The IP address to listen to.",                "IP",          "all IP addresses IPv4 and IPv6"},
 272    { 'p', "API/Web port to use.",                        "port",        "19999"},
 273    { 's', "Prefix for /proc and /sys (for containers).", "path",        "no prefix"},
 274    { 't', "The internal clock of netdata.",              "seconds",     "1"},
 275    { 'u', "Run as user.",                                "username",    "netdata"},
 276    { 'v', "Print netdata version and exit.",             NULL,          NULL},
 277    { 'V', "Print netdata version and exit.",             NULL,          NULL},
 278    { 'W', "See Advanced options below.",                 "options",     NULL},

默认的启动脚本:

ExecStart=/usr/sbin/net data -P /var/run/netdata/netdata.pid -D -W set global 'process scheduling policy' 'keep' -W set global 'OOM score' 'keep'

多了几个参数,使用 -W 进行设置。待补充

配置文件解析

section

配置包含了以下几个section

 85#define CONFIG_SECTION_GLOBAL     "global"
86#define CONFIG_SECTION_WEB        "web"
87#define CONFIG_SECTION_STATSD     "statsd"
88#define CONFIG_SECTION_PLUGINS    "plugins"
89#define CONFIG_SECTION_CLOUD      "cloud"
90#define CONFIG_SECTION_REGISTRY   "registry"
91#define CONFIG_SECTION_HEALTH     "health"
92#define CONFIG_SECTION_BACKEND    "backend"
93#define CONFIG_SECTION_STREAM     "stream"
94#define CONFIG_SECTION_EXPORTING  "exporting:global"
95#define CONFIG_SECTION_PROMETHEUS "prometheus:exporter"
96#define CONFIG_SECTION_HOST_LABEL "host labels"
97#define EXPORTING_CONF            "exporting.conf"

每个section下有若干key-value组成。

结构

配置文件由一个avl树来进行维护。

143struct config {
144    struct section *first_section;
145    struct section *last_section; // optimize inserting at the end
146    netdata_mutex_t mutex;
147    avl_tree_lock index;
148};

mutex 是一个 pthread_mutex_t互斥量,主要用来做写锁 。另一个锁是avl的锁,主要用来读锁。

126struct section {
127    avl avl;                // the index entry of this section - this has to be first!
128
129    uint32_t hash;          // a simple hash to speed up searching
130                            // we first compare hashes, and only if the hashes are equal we do string comparisons
131
132    char *name;
133
134    struct section *next;    // gloabl config_mutex protects just this
135
136    struct config_option *values;
137    avl_tree_lock values_index;
138
139    netdata_mutex_t mutex;  // this locks only the writers, to ensure atomic updates
140                            // readers are protected using the rwlock in avl_tree_lock
141};

hash使用的是FNV hash算法。

比较

112static int appconfig_option_compare(void *a, void *b) {
113    if(((struct config_option *)a)->hash < ((struct config_option *)b)->hash) return -1;
114    else if(((struct config_option *)a)->hash > ((struct config_option *)b)->hash) return 1;
115    else return strcmp(((struct config_option *)a)->name, ((struct config_option *)b)->name);
116}

这里先比较hash ,再比较name。

【未完待续】

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!