]> arthur.barton.de Git - netdata.git/blob - src/procfile.h
Merge pull request #1998 from ktsaou/master
[netdata.git] / src / procfile.h
1 /*
2  * procfile is a library for reading kernel files from /proc
3  *
4  * The idea is this:
5  *
6  *  - every file is opened once with procfile_open().
7  *
8  *  - to read updated contents, we rewind it (lseek() to 0) and read again
9  *    with procfile_readall().
10  *
11  *  - for every file, we use a buffer that is adjusted to fit its entire
12  *    contents in memory, allowing us to read it with a single read() call.
13  *    (this provides atomicity / consistency on the data read from the kernel)
14  *
15  *  - once the data are read, we update two arrays of pointers:
16  *     - a words array, pointing to each word in the data read
17  *     - a lines array, pointing to the first word for each line
18  *
19  *    This is highly optimized. Both arrays are automatically adjusted to
20  *    fit all contents and are updated in a single pass on the data:
21  *     - a raspberry Pi can process 5.000+ files / sec.
22  *     - a J1900 celeron processor can process 23.000+ files / sec.
23 */
24
25
26 #ifndef NETDATA_PROCFILE_H
27 #define NETDATA_PROCFILE_H 1
28
29 // ----------------------------------------------------------------------------
30 // An array of words
31
32 typedef struct {
33     size_t len;     // used entries
34     size_t size;    // capacity
35     char *words[];  // array of pointers
36 } pfwords;
37
38
39 // ----------------------------------------------------------------------------
40 // An array of lines
41
42 typedef struct {
43     size_t words;   // how many words this line has
44     size_t first;   // the id of the first word of this line
45                     // in the words array
46 } ffline;
47
48 typedef struct {
49     size_t len;     // used entries
50     size_t size;    // capacity
51     ffline lines[]; // array of lines
52 } pflines;
53
54
55 // ----------------------------------------------------------------------------
56 // The procfile
57
58 #define PROCFILE_FLAG_DEFAULT             0x00000000
59 #define PROCFILE_FLAG_NO_ERROR_ON_FILE_IO 0x00000001
60
61 typedef enum procfile_separator {
62     PF_CHAR_IS_SEPARATOR,
63     PF_CHAR_IS_NEWLINE,
64     PF_CHAR_IS_WORD,
65     PF_CHAR_IS_QUOTE,
66     PF_CHAR_IS_OPEN,
67     PF_CHAR_IS_CLOSE
68 } PF_CHAR_TYPE;
69
70 typedef struct {
71     char filename[FILENAME_MAX + 1]; // not populated until profile_filename() is called
72
73     uint32_t flags;
74     int fd;               // the file desriptor
75     size_t len;           // the bytes we have placed into data
76     size_t size;          // the bytes we have allocated for data
77     pflines *lines;
78     pfwords *words;
79     PF_CHAR_TYPE separators[256];
80     char data[];          // allocated buffer to keep file contents
81 } procfile;
82
83 // close the proc file and free all related memory
84 extern void procfile_close(procfile *ff);
85
86 // (re)read and parse the proc file
87 extern procfile *procfile_readall(procfile *ff);
88
89 // open a /proc or /sys file
90 extern procfile *procfile_open(const char *filename, const char *separators, uint32_t flags);
91
92 // re-open a file
93 // if separators == NULL, the last separators are used
94 extern procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags);
95
96 // example walk-through a procfile parsed file
97 extern void procfile_print(procfile *ff);
98
99 extern void procfile_set_quotes(procfile *ff, const char *quotes);
100 extern void procfile_set_open_close(procfile *ff, const char *open, const char *close);
101
102 extern char *procfile_filename(procfile *ff);
103
104 // ----------------------------------------------------------------------------
105
106 // set this to 1, to have procfile adapt its initial buffer allocation to the max allocation used so far
107 extern int procfile_adaptive_initial_allocation;
108
109 // return the number of lines present
110 #define procfile_lines(ff) (ff->lines->len)
111
112 // return the number of words of the Nth line
113 #define procfile_linewords(ff, line) (((line) < procfile_lines(ff)) ? (ff)->lines->lines[(line)].words : 0)
114
115 // return the Nth word of the file, or empty string
116 #define procfile_word(ff, word) (((word) < (ff)->words->len) ? (ff)->words->words[(word)] : "")
117
118 // return the first word of the Nth line, or empty string
119 #define procfile_line(ff, line) (((line) < procfile_lines(ff)) ? procfile_word((ff), (ff)->lines->lines[(line)].first) : "")
120
121 // return the Nth word of the current line
122 #define procfile_lineword(ff, line, word) (((line) < procfile_lines(ff) && (word) < procfile_linewords(ff, (line))) ? procfile_word((ff), (ff)->lines->lines[(line)].first + word) : "")
123
124 #endif /* NETDATA_PROCFILE_H */