0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-27 14:16:20 +00:00
netdata_netdata/collectors/proc.plugin/sys_block_zram.c
Costa Tsaousis cb7af25c09
RRD structures managed by dictionaries ()
* rrdset - in progress

* rrdset optimal constructor; rrdset conflict

* rrdset final touches

* re-organization of rrdset object members

* prevent use-after-free

* dictionary dfe supports also counting of iterations

* rrddim managed by dictionary

* rrd.h cleanup

* DICTIONARY_ITEM now is referencing actual dictionary items in the code

* removed rrdset linked list

* Revert "removed rrdset linked list"

This reverts commit 690d6a588b4b99619c2c5e10f84e8f868ae6def5.

* removed rrdset linked list

* added comments

* Switch chart uuid to static allocation in rrdset
Remove unused functions

* rrdset_archive() and friends...

* always create rrdfamily

* enable ml_free_dimension

* rrddim_foreach done with dfe

* most custom rrddim loops replaced with rrddim_foreach

* removed accesses to rrddim->dimensions

* removed locks that are no longer needed

* rrdsetvar is now managed by the dictionary

* set rrdset is rrdsetvar, fixes https://github.com/netdata/netdata/pull/13646#issuecomment-1242574853

* conflict callback of rrdsetvar now properly checks if it has to reset the variable

* dictionary registered callbacks accept as first parameter the DICTIONARY_ITEM

* dictionary dfe now uses internal counter to report; avoided excess variables defined with dfe

* dictionary walkthrough callbacks get dictionary acquired items

* dictionary reference counters that can be dupped from zero

* added advanced functions for get and del

* rrdvar managed by dictionaries

* thread safety for rrdsetvar

* faster rrdvar initialization

* rrdvar string lengths should match in all add, del, get functions

* rrdvar internals hidden from the rest of the world

* rrdvar is now acquired throughout netdata

* hide the internal structures of rrdsetvar

* rrdsetvar is now acquired through out netdata

* rrddimvar managed by dictionary; rrddimvar linked list removed; rrddimvar structures hidden from the rest of netdata

* better error handling

* dont create variables if not initialized for health

* dont create variables if not initialized for health again

* rrdfamily is now managed by dictionaries; references of it are acquired dictionary items

* type checking on acquired objects

* rrdcalc renaming of functions

* type checking for rrdfamily_acquired

* rrdcalc managed by dictionaries

* rrdcalc double free fix

* host rrdvars is always needed

* attempt to fix deadlock 1

* attempt to fix deadlock 2

* Remove unused variable

* attempt to fix deadlock 3

* snprintfz

* rrdcalc index in rrdset fix

* Stop storing active charts and computing chart hashes

* Remove store active chart function

* Remove compute chart hash function

* Remove sql_store_chart_hash function

* Remove store_active_dimension function

* dictionary delayed destruction

* formatting and cleanup

* zero dictionary base on rrdsetvar

* added internal error to log delayed destructions of dictionaries

* typo in rrddimvar

* added debugging info to dictionary

* debug info

* fix for rrdcalc keys being empty

* remove forgotten unlock

* remove deadlock

* Switch to metadata version 5 and drop
  chart_hash
  chart_hash_map
  chart_active
  dimension_active
  v_chart_hash

* SQL cosmetic changes

* do not busy wait while destroying a referenced dictionary

* remove deadlock

* code cleanup; re-organization;

* fast cleanup and flushing of dictionaries

* number formatting fixes

* do not delete configured alerts when archiving a chart

* rrddim obsolete linked list management outside dictionaries

* removed duplicate contexts call

* fix crash when rrdfamily is not initialized

* dont keep rrddimvar referenced

* properly cleanup rrdvar

* removed some locks

* Do not attempt to cleanup chart_hash / chart_hash_map

* rrdcalctemplate managed by dictionary

* register callbacks on the right dictionary

* removed some more locks

* rrdcalc secondary index replaced with linked-list; rrdcalc labels updates are now executed by health thread

* when looking up for an alarm look using both chart id and chart name

* host initialization a bit more modular

* init rrdlabels on host update

* preparation for dictionary views

* improved comment

* unused variables without internal checks

* service threads isolation and worker info

* more worker info in service thread

* thread cancelability debugging with internal checks

* strings data races addressed; fixes https://github.com/netdata/netdata/issues/13647

* dictionary modularization

* Remove unused SQL statement definition

* unit-tested thread safety of dictionaries; removed data race conditions on dictionaries and strings; dictionaries now can detect if the caller is holds a write lock and automatically all the calls become their unsafe versions; all direct calls to unsafe version is eliminated

* remove worker_is_idle() from the exit of service functions, because we lose the lock time between loops

* rewritten dictionary to have 2 separate locks, one for indexing and another for traversal

* Update collectors/cgroups.plugin/sys_fs_cgroup.c

Co-authored-by: Vladimir Kobal <vlad@prokk.net>

* Update collectors/cgroups.plugin/sys_fs_cgroup.c

Co-authored-by: Vladimir Kobal <vlad@prokk.net>

* Update collectors/proc.plugin/proc_net_dev.c

Co-authored-by: Vladimir Kobal <vlad@prokk.net>

* fix memory leak in rrdset cache_dir

* minor dictionary changes

* dont use index locks in single threaded

* obsolete dict option

* rrddim options and flags separation; rrdset_done() optimization to keep array of reference pointers to rrddim;

* fix jump on uninitialized value in dictionary; remove double free of cache_dir

* addressed codacy findings

* removed debugging code

* use the private refcount on dictionaries

* make dictionary item desctructors work on dictionary destruction; strictier control on dictionary API; proper cleanup sequence on rrddim;

* more dictionary statistics

* global statistics about dictionary operations, memory, items, callbacks

* dictionary support for views - missing the public API

* removed warning about unused parameter

* chart and context name for cloud

* chart and context name for cloud, again

* dictionary statistics fixed; first implementation of dictionary views - not currently used

* only the master can globally delete an item

* context needs netdata prefix

* fix context and chart it of spins

* fix for host variables when health is not enabled

* run garbage collector on item insert too

* Fix info message; remove extra "using"

* update dict unittest for new placement of garbage collector

* we need RRDHOST->rrdvars for maintaining custom host variables

* Health initialization needs the host->host_uuid

* split STRING to its own files; no code changes other than that

* initialize health unconditionally

* unit tests do not pollute the global scope with their variables

* Skip initialization when creating archived hosts on startup. When a child connects it will initialize properly

Co-authored-by: Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com>
Co-authored-by: Vladimir Kobal <vlad@prokk.net>
2022-09-19 23:46:13 +03:00

299 lines
No EOL
9.9 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
#include "plugin_proc.h"
#define PLUGIN_PROC_MODULE_ZRAM_NAME "/sys/block/zram"
#define rrdset_obsolete_and_pointer_null(st) do { if(st) { rrdset_is_obsolete(st); (st) = NULL; } } while(st)
typedef struct mm_stat {
unsigned long long orig_data_size;
unsigned long long compr_data_size;
unsigned long long mem_used_total;
unsigned long long mem_limit;
unsigned long long mem_used_max;
unsigned long long same_pages;
unsigned long long pages_compacted;
} MM_STAT;
typedef struct zram_device {
procfile *file;
RRDSET *st_usage;
RRDDIM *rd_compr_data_size;
RRDDIM *rd_metadata_size;
RRDSET *st_savings;
RRDDIM *rd_original_size;
RRDDIM *rd_savings_size;
RRDSET *st_comp_ratio;
RRDDIM *rd_comp_ratio;
RRDSET *st_alloc_efficiency;
RRDDIM *rd_alloc_efficiency;
} ZRAM_DEVICE;
// --------------------------------------------------------------------
static int try_get_zram_major_number(procfile *file) {
size_t i;
unsigned int lines = procfile_lines(file);
int id = -1;
char *name = NULL;
for (i = 0; i < lines; i++)
{
if (procfile_linewords(file, i) < 2)
continue;
name = procfile_lineword(file, i, 1);
if (strcmp(name, "zram") == 0)
{
id = str2i(procfile_lineword(file, i, 0));
if (id == 0)
return -1;
return id;
}
}
return -1;
}
static inline void init_rrd(const char *name, ZRAM_DEVICE *d, int update_every) {
char chart_name[RRD_ID_LENGTH_MAX + 1];
snprintfz(chart_name, RRD_ID_LENGTH_MAX, "zram_usage.%s", name);
d->st_usage = rrdset_create_localhost(
"mem"
, chart_name
, chart_name
, name
, "mem.zram_usage"
, "ZRAM Memory Usage"
, "MiB"
, PLUGIN_PROC_NAME
, PLUGIN_PROC_MODULE_ZRAM_NAME
, NETDATA_CHART_PRIO_MEM_ZRAM
, update_every
, RRDSET_TYPE_AREA);
d->rd_compr_data_size = rrddim_add(d->st_usage, "compressed", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
d->rd_metadata_size = rrddim_add(d->st_usage, "metadata", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
rrdlabels_add(d->st_usage->rrdlabels, "device", name, RRDLABEL_SRC_AUTO);
snprintfz(chart_name, RRD_ID_LENGTH_MAX, "zram_savings.%s", name);
d->st_savings = rrdset_create_localhost(
"mem"
, chart_name
, chart_name
, name
, "mem.zram_savings"
, "ZRAM Memory Savings"
, "MiB"
, PLUGIN_PROC_NAME
, PLUGIN_PROC_MODULE_ZRAM_NAME
, NETDATA_CHART_PRIO_MEM_ZRAM_SAVINGS
, update_every
, RRDSET_TYPE_AREA);
d->rd_savings_size = rrddim_add(d->st_savings, "savings", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
d->rd_original_size = rrddim_add(d->st_savings, "original", NULL, 1, 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
rrdlabels_add(d->st_savings->rrdlabels, "device", name, RRDLABEL_SRC_AUTO);
snprintfz(chart_name, RRD_ID_LENGTH_MAX, "zram_ratio.%s", name);
d->st_comp_ratio = rrdset_create_localhost(
"mem"
, chart_name
, chart_name
, name
, "mem.zram_ratio"
, "ZRAM Compression Ratio (original to compressed)"
, "ratio"
, PLUGIN_PROC_NAME
, PLUGIN_PROC_MODULE_ZRAM_NAME
, NETDATA_CHART_PRIO_MEM_ZRAM_RATIO
, update_every
, RRDSET_TYPE_LINE);
d->rd_comp_ratio = rrddim_add(d->st_comp_ratio, "ratio", NULL, 1, 100, RRD_ALGORITHM_ABSOLUTE);
rrdlabels_add(d->st_comp_ratio->rrdlabels, "device", name, RRDLABEL_SRC_AUTO);
snprintfz(chart_name, RRD_ID_LENGTH_MAX, "zram_efficiency.%s", name);
d->st_alloc_efficiency = rrdset_create_localhost(
"mem"
, chart_name
, chart_name
, name
, "mem.zram_efficiency"
, "ZRAM Efficiency"
, "percentage"
, PLUGIN_PROC_NAME
, PLUGIN_PROC_MODULE_ZRAM_NAME
, NETDATA_CHART_PRIO_MEM_ZRAM_EFFICIENCY
, update_every
, RRDSET_TYPE_LINE);
d->rd_alloc_efficiency = rrddim_add(d->st_alloc_efficiency, "percent", NULL, 1, 10000, RRD_ALGORITHM_ABSOLUTE);
rrdlabels_add(d->st_alloc_efficiency->rrdlabels, "device", name, RRDLABEL_SRC_AUTO);
}
static int init_devices(DICTIONARY *devices, unsigned int zram_id, int update_every) {
int count = 0;
DIR *dir = opendir("/dev");
struct dirent *de;
struct stat st;
char filename[FILENAME_MAX + 1];
procfile *ff = NULL;
ZRAM_DEVICE device;
if (unlikely(!dir))
return 0;
while ((de = readdir(dir)))
{
snprintfz(filename, FILENAME_MAX, "/dev/%s", de->d_name);
if (unlikely(stat(filename, &st) != 0))
{
error("ZRAM : Unable to stat %s: %s", filename, strerror(errno));
continue;
}
if (major(st.st_rdev) == zram_id)
{
info("ZRAM : Found device %s", filename);
snprintfz(filename, FILENAME_MAX, "/sys/block/%s/mm_stat", de->d_name);
ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
if (ff == NULL)
{
error("ZRAM : Failed to open %s: %s", filename, strerror(errno));
continue;
}
device.file = ff;
init_rrd(de->d_name, &device, update_every);
dictionary_set(devices, de->d_name, &device, sizeof(ZRAM_DEVICE));
count++;
}
}
closedir(dir);
return count;
}
static void free_device(DICTIONARY *dict, const char *name)
{
ZRAM_DEVICE *d = (ZRAM_DEVICE*)dictionary_get(dict, name);
info("ZRAM : Disabling monitoring of device %s", name);
rrdset_obsolete_and_pointer_null(d->st_usage);
rrdset_obsolete_and_pointer_null(d->st_savings);
rrdset_obsolete_and_pointer_null(d->st_alloc_efficiency);
rrdset_obsolete_and_pointer_null(d->st_comp_ratio);
dictionary_del(dict, name);
}
// --------------------------------------------------------------------
static inline int read_mm_stat(procfile *ff, MM_STAT *stats) {
ff = procfile_readall(ff);
if (!ff)
return -1;
if (procfile_lines(ff) < 1) {
procfile_close(ff);
return -1;
}
if (procfile_linewords(ff, 0) < 7) {
procfile_close(ff);
return -1;
}
stats->orig_data_size = str2ull(procfile_word(ff, 0));
stats->compr_data_size = str2ull(procfile_word(ff, 1));
stats->mem_used_total = str2ull(procfile_word(ff, 2));
stats->mem_limit = str2ull(procfile_word(ff, 3));
stats->mem_used_max = str2ull(procfile_word(ff, 4));
stats->same_pages = str2ull(procfile_word(ff, 5));
stats->pages_compacted = str2ull(procfile_word(ff, 6));
return 0;
}
static inline int _collect_zram_metrics(const char* name, ZRAM_DEVICE *d, int advance, DICTIONARY* dict) {
MM_STAT mm;
int value;
if (unlikely(read_mm_stat(d->file, &mm) < 0))
{
free_device(dict, name);
return -1;
}
if (likely(advance))
{
rrdset_next(d->st_usage);
rrdset_next(d->st_savings);
rrdset_next(d->st_comp_ratio);
rrdset_next(d->st_alloc_efficiency);
}
// zram_usage
rrddim_set_by_pointer(d->st_usage, d->rd_compr_data_size, mm.compr_data_size);
rrddim_set_by_pointer(d->st_usage, d->rd_metadata_size, mm.mem_used_total - mm.compr_data_size);
rrdset_done(d->st_usage);
// zram_savings
rrddim_set_by_pointer(d->st_savings, d->rd_savings_size, mm.compr_data_size - mm.orig_data_size);
rrddim_set_by_pointer(d->st_savings, d->rd_original_size, mm.orig_data_size);
rrdset_done(d->st_savings);
// zram_ratio
value = mm.compr_data_size == 0 ? 1 : mm.orig_data_size * 100 / mm.compr_data_size;
rrddim_set_by_pointer(d->st_comp_ratio, d->rd_comp_ratio, value);
rrdset_done(d->st_comp_ratio);
// zram_efficiency
value = mm.mem_used_total == 0 ? 100 : (mm.compr_data_size * 1000000 / mm.mem_used_total);
rrddim_set_by_pointer(d->st_alloc_efficiency, d->rd_alloc_efficiency, value);
rrdset_done(d->st_alloc_efficiency);
return 0;
}
static int collect_first_zram_metrics(const DICTIONARY_ITEM *item, void *entry, void *data) {
const char *name = dictionary_acquired_item_name(item);
// collect without calling rrdset_next (init only)
return _collect_zram_metrics(name, (ZRAM_DEVICE *)entry, 0, (DICTIONARY *)data);
}
static int collect_zram_metrics(const DICTIONARY_ITEM *item, void *entry, void *data) {
const char *name = dictionary_acquired_item_name(item);
// collect with calling rrdset_next
return _collect_zram_metrics(name, (ZRAM_DEVICE *)entry, 1, (DICTIONARY *)data);
}
// --------------------------------------------------------------------
int do_sys_block_zram(int update_every, usec_t dt) {
(void)dt;
static procfile *ff = NULL;
static DICTIONARY *devices = NULL;
static int initialized = 0;
static int device_count = 0;
int zram_id = -1;
if (unlikely(!initialized))
{
initialized = 1;
ff = procfile_open("/proc/devices", " \t:", PROCFILE_FLAG_DEFAULT);
if (ff == NULL)
{
error("Cannot read /proc/devices");
return 1;
}
ff = procfile_readall(ff);
if (!ff)
return 1;
zram_id = try_get_zram_major_number(ff);
if (zram_id == -1)
{
if (ff != NULL)
procfile_close(ff);
return 1;
}
procfile_close(ff);
devices = dictionary_create(DICT_OPTION_SINGLE_THREADED);
device_count = init_devices(devices, (unsigned int)zram_id, update_every);
if (device_count < 1)
return 1;
dictionary_walkthrough_write(devices, collect_first_zram_metrics, devices);
}
else
{
if (unlikely(device_count < 1))
return 1;
dictionary_walkthrough_write(devices, collect_zram_metrics, devices);
}
return 0;
}