- rrd_open() calculates file size for new files and calls mmap once for
[rrdtool.git] / src / rrd.h
1 /*****************************************************************************
2  * RRDtool 1.3.2  Copyright by Tobi Oetiker, 1997-2008
3  *****************************************************************************
4  * rrdlib.h   Public header file for librrd
5  *****************************************************************************
6  * $Id$
7  * $Log$
8  * Revision 1.9  2005/02/13 16:13:33  oetiker
9  * let rrd_graph return the actual value range it picked ...
10  * -- Henrik Stoerner <henrik@hswn.dk>
11  *
12  * Revision 1.8  2004/05/26 22:11:12  oetiker
13  * reduce compiler warnings. Many small fixes. -- Mike Slifcak <slif@bellsouth.net>
14  *
15  * Revision 1.7  2003/11/12 22:14:26  oetiker
16  * allow to pass an open filehandle into rrd_graph as an extra argument
17  *
18  * Revision 1.6  2003/11/11 19:46:21  oetiker
19  * replaced time_value with rrd_time_value as MacOS X introduced a struct of that name in their standard headers
20  *
21  * Revision 1.5  2003/04/25 18:35:08  jake
22  * Alternate update interface, updatev. Returns info about CDPs written to disk as result of update. Output format is similar to rrd_info, a hash of key-values.
23  *
24  * Revision 1.4  2003/04/01 22:52:23  jake
25  * Fix Win32 build. VC++ 6.0 and 7.0 now use the thread-safe code.
26  *
27  * Revision 1.3  2003/02/13 07:05:27  oetiker
28  * Find attached the patch I promised to send to you. Please note that there
29  * are three new source files (src/rrd_is_thread_safe.h, src/rrd_thread_safe.c
30  * and src/rrd_not_thread_safe.c) and the introduction of librrd_th. This
31  * library is identical to librrd, but it contains support code for per-thread
32  * global variables currently used for error information only. This is similar
33  * to how errno per-thread variables are implemented.  librrd_th must be linked
34  * alongside of libpthred
35  *
36  * There is also a new file "THREADS", holding some documentation.
37  *
38  * -- Peter Stamfest <peter@stamfest.at>
39  *
40  * Revision 1.2  2002/05/07 21:58:32  oetiker
41  * new command rrdtool xport integrated
42  * --  Wolfgang Schrimm <Wolfgang.Schrimm@urz.uni-heidelberg.de>
43  *
44  * Revision 1.1.1.1  2001/02/25 22:25:05  oetiker
45  * checkin
46  *
47  *****************************************************************************/
48 #ifdef  __cplusplus
49 extern    "C" {
50 #endif
51
52 #ifndef _RRDLIB_H
53 #define _RRDLIB_H
54
55 #include <sys/types.h>  /* for off_t */
56 #include <unistd.h>     /* for off_t */
57 #include <time.h>
58 #include <stdio.h>      /* for FILE */
59
60
61 /* Formerly rrd_nan_inf.h */
62 #ifndef DNAN
63 # define DNAN rrd_set_to_DNAN()
64 #endif
65
66 #ifndef DINF
67 # define DINF rrd_set_to_DINF()
68 #endif
69     double    rrd_set_to_DNAN(
70     void);
71     double    rrd_set_to_DINF(
72     void);
73 /* end of rrd_nan_inf.h */
74
75 /* Transplanted from rrd_format.h */
76     typedef double rrd_value_t; /* the data storage type is
77                                  * double */
78 /* END rrd_format.h */
79
80 /* information about an rrd file */
81     typedef struct rrd_file_t {
82         int       fd;   /* file descriptor if this rrd file */
83         char     *file_start;   /* start address of an open rrd file */
84         off_t     header_len;   /* length of the header of this rrd file */
85         off_t     file_len; /* total size of the rrd file */
86         off_t     pos;  /* current pos in file */
87 #ifdef HAVE_MMAP
88         int       mm_prot;
89         int       mm_flags;
90 #endif
91     } rrd_file_t;
92
93 /* rrd info interface */
94     typedef struct rrd_blob_t {
95         unsigned long size; /* size of the blob */
96         unsigned char *ptr; /* pointer */
97     } rrd_blob_t;
98
99     typedef enum rrd_info_type { RD_I_VAL = 0,
100         RD_I_CNT,
101         RD_I_STR,
102         RD_I_INT,
103         RD_I_BLO
104     } rrd_info_type_t;
105
106     typedef union rrd_infoval {
107         unsigned long u_cnt;
108         rrd_value_t u_val;
109         char     *u_str;
110         int       u_int;
111         rrd_blob_t u_blo;
112     } rrd_infoval_t;
113
114     typedef struct rrd_info_t {
115         char     *key;
116         rrd_info_type_t type;
117         rrd_infoval_t value;
118         struct rrd_info_t *next;
119     } rrd_info_t;
120
121
122 /* main function blocks */
123     int       rrd_create(
124     int,
125     char **);
126     rrd_info_t *rrd_info(
127     int,
128     char **);
129     rrd_info_t *rrd_info_push(
130     rrd_info_t *,
131     char *,
132     rrd_info_type_t,
133     rrd_infoval_t);
134     void      rrd_info_print(
135     rrd_info_t * data);
136     void      rrd_info_free(
137     rrd_info_t *);
138     int       rrd_update(
139     int,
140     char **);
141     rrd_info_t *rrd_update_v(
142     int,
143     char **);
144     int       rrd_graph(
145     int,
146     char **,
147     char ***,
148     int *,
149     int *,
150     FILE *,
151     double *,
152     double *);
153     rrd_info_t *rrd_graph_v(
154     int,
155     char **);
156
157     int       rrd_fetch(
158     int,
159     char **,
160     time_t *,
161     time_t *,
162     unsigned long *,
163     unsigned long *,
164     char ***,
165     rrd_value_t **);
166     int       rrd_restore(
167     int,
168     char **);
169     int       rrd_dump(
170     int,
171     char **);
172     int       rrd_tune(
173     int,
174     char **);
175     time_t    rrd_last(
176     int,
177     char **);
178     int rrd_lastupdate(int argc, char **argv);
179     time_t    rrd_first(
180     int,
181     char **);
182     int       rrd_resize(
183     int,
184     char **);
185     char     *rrd_strversion(
186     void);
187     double    rrd_version(
188     void);
189     int       rrd_xport(
190     int,
191     char **,
192     int *,
193     time_t *,
194     time_t *,
195     unsigned long *,
196     unsigned long *,
197     char ***,
198     rrd_value_t **);
199     int       rrd_cmd_flush (int argc, char **argv);
200
201     void      rrd_freemem(
202     void *mem);
203
204 /* thread-safe (hopefully) */
205     int       rrd_create_r(
206     const char *filename,
207     unsigned long pdp_step,
208     time_t last_up,
209     int argc,
210     const char **argv);
211     rrd_info_t *rrd_info_r(
212     char *);
213 /* NOTE: rrd_update_r are only thread-safe if no at-style time
214    specifications get used!!! */
215
216     int       rrd_update_r(
217     const char *filename,
218     const char *_template,
219     int argc,
220     const char **argv);
221     int rrd_fetch_r (
222             const char *filename,
223             const char *cf,
224             time_t *start,
225             time_t *end,
226             unsigned long *step,
227             unsigned long *ds_cnt,
228             char ***ds_namv,
229             rrd_value_t **data);
230     int       rrd_dump_r(
231     const char *filename,
232     char *outname);
233     time_t    rrd_last_r (const char *filename);
234     int rrd_lastupdate_r (const char *filename,
235             time_t *ret_last_update,
236             unsigned long *ret_ds_count,
237             char ***ret_ds_names,
238             char ***ret_last_ds);
239     time_t    rrd_first_r(
240     const char *filename,
241     int rraindex);
242
243 /* Transplanted from rrd_parsetime.h */
244     typedef enum {
245         ABSOLUTE_TIME,
246         RELATIVE_TO_START_TIME,
247         RELATIVE_TO_END_TIME
248     } rrd_timetype_t;
249
250 #define TIME_OK NULL
251
252     typedef struct rrd_time_value {
253         rrd_timetype_t type;
254         long      offset;
255         struct tm tm;
256     } rrd_time_value_t;
257
258     char     *rrd_parsetime(
259     const char *spec,
260     rrd_time_value_t * ptv);
261 /* END rrd_parsetime.h */
262
263     typedef struct rrd_context {
264         char      lib_errstr[256];
265         char      rrd_error[4096];
266     } rrd_context_t;
267
268 /* returns the current per-thread rrd_context */
269     rrd_context_t *rrd_get_context(
270     void);
271
272
273     int       rrd_proc_start_end(
274     rrd_time_value_t *,
275     rrd_time_value_t *,
276     time_t *,
277     time_t *);
278
279 /* HELPER FUNCTIONS */
280     void      rrd_set_error(
281     char *,
282     ...);
283     void      rrd_clear_error(
284     void);
285     int       rrd_test_error(
286     void);
287     char     *rrd_get_error(
288     void);
289
290     /* rrd_strerror is thread safe, but still it uses a global buffer
291        (but one per thread), thus subsequent calls within a single
292        thread overwrite the same buffer */
293     const char *rrd_strerror(
294     int err);
295
296 /** MULTITHREADED HELPER FUNCTIONS */
297     rrd_context_t *rrd_new_context(
298     void);
299     void      rrd_free_context(
300     rrd_context_t * buf);
301
302 /* void   rrd_set_error_r  (rrd_context_t *, char *, ...); */
303 /* void   rrd_clear_error_r(rrd_context_t *); */
304 /* int    rrd_test_error_r (rrd_context_t *); */
305 /* char  *rrd_get_error_r  (rrd_context_t *); */
306
307 /*
308  * The following functions are _internal_ functions needed to read the raw RRD
309  * files. Since they are _internal_ they may change with the file format and
310  * will be replaced with a more general interface in RRDTool 1.4. Don't use
311  * these functions unless you have good reasons to do so. If you do use these
312  * functions you will have to adapt your code for RRDTool 1.4!
313  *
314  * To enable the deprecated functions define `RRD_EXPORT_DEPRECATED' before
315  * including <rrd_test.h>. You have been warned! If you come back to the
316  * RRDTool mailing list and whine about your broken application, you will get
317  * hit with something smelly!
318  */
319 #if defined(_RRD_TOOL_H) || defined(RRD_EXPORT_DEPRECATED)
320
321 # if defined(_RRD_TOOL_H)
322 #  include "rrd_format.h"
323 # else
324 #  include <rrd_format.h>
325 # endif
326
327 #if defined(__GNUC__) && defined (RRD_EXPORT_DEPRECATED)
328 # define RRD_DEPRECATED __attribute__((deprecated))
329 #else
330 # define RRD_DEPRECATED          /**/
331 #endif
332      void     rrd_free(
333     rrd_t *rrd)
334               RRD_DEPRECATED;
335     void      rrd_init(
336     rrd_t *rrd)
337               RRD_DEPRECATED;
338
339     rrd_file_t *rrd_open(
340     const char *const file_name,
341     rrd_t *rrd,
342     unsigned rdwr)
343               RRD_DEPRECATED;
344
345     void      rrd_dontneed(
346     rrd_file_t *rrd_file,
347     rrd_t *rrd)
348               RRD_DEPRECATED;
349     int       rrd_close(
350     rrd_file_t *rrd_file)
351               RRD_DEPRECATED;
352     ssize_t   rrd_read(
353     rrd_file_t *rrd_file,
354     void *buf,
355     size_t count)
356               RRD_DEPRECATED;
357     ssize_t   rrd_write(
358     rrd_file_t *rrd_file,
359     const void *buf,
360     size_t count)
361               RRD_DEPRECATED;
362     void      rrd_flush(
363     rrd_file_t *rrd_file)
364               RRD_DEPRECATED;
365     off_t     rrd_seek(
366     rrd_file_t *rrd_file,
367     off_t off,
368     int whence)
369               RRD_DEPRECATED;
370     off_t     rrd_tell(
371     rrd_file_t *rrd_file)
372               RRD_DEPRECATED;
373     int       rrd_lock(
374     rrd_file_t *file)
375               RRD_DEPRECATED;
376 #endif                  /* defined(_RRD_TOOL_H) || defined(RRD_EXPORT_DEPRECATED) */
377
378 #endif                  /* _RRDLIB_H */
379
380 #ifdef  __cplusplus
381 }
382 #endif