]>
jfr.im git - irc/quakenet/newserv.git/blob - graphing/fsample.c
11 static fsample_t version
= 1;
13 static void fscorefree(void *arg
) {
14 fsample
*f
= (fsample
*)arg
;
15 munmap(f
->header
, f
->len
);
18 fsample
*fsopen(char *filename
, size_t samples
, CoreHandlerAddFn chafn
, CoreHandlerDelFn chdfn
) {
21 fsample
*f
= (fsample
*)malloc(sizeof(fsample
));
25 f
->len
= samples
* sizeof(struct fsample_p
) + sizeof(struct fsample_header
);
26 f
->fd
= open(filename
, O_RDWR
|O_CREAT
, 0600);
28 if(!lseek(f
->fd
, 0, SEEK_END
))
31 if((f
->fd
== -1) || (lseek(f
->fd
, f
->len
- 1, SEEK_SET
) == -1)) {
42 f
->header
= mmap(NULL
, f
->len
, PROT_READ
|PROT_WRITE
, flags
, f
->fd
, 0);
43 if(f
->header
== MAP_FAILED
) {
49 if(!new && (memcmp(f
->header
->magic
, "FSAMP", sizeof(f
->header
->magic
)) || (version
!= f
->header
->version
))) {
50 munmap(f
->header
, f
->len
);
57 f
->corehandler
= chafn(fscorefree
, f
);
58 f
->corehandlerdel
= chdfn
;
60 f
->corehandler
= NULL
;
61 f
->corehandlerdel
= NULL
;
64 memcpy(f
->header
->magic
, "FSAMP", sizeof(f
->header
->magic
));
65 f
->header
->version
= version
;
67 f
->m
= (struct fsample_p
*)(f
->header
+ 1);
70 if(f
->header
->iteration
== 0)
71 f
->header
->iteration
= 1;
76 void fsclose(fsample
*f
) {
77 munmap(f
->header
, f
->len
);
80 if(f
->corehandlerdel
&& f
->corehandler
)
81 (f
->corehandlerdel
)(f
->corehandler
);
86 int fsadd_m(fsample_m
*f
, char *filename
, size_t freq
, DeriveValueFn derive
, void *tag
) {
87 fsample_m_entry
*p
= &f
->entry
[f
->pos
];
89 p
->f
= fsopen(filename
, f
->samples
/ freq
, f
->chafn
, f
->chdfn
);
100 fsample_m
*fsopen_m(size_t count
, char *filename
, size_t samples
, CoreHandlerAddFn chafn
, CoreHandlerDelFn chdfn
) {
101 fsample_m
*n
= (fsample_m
*)malloc(sizeof(fsample_m
) + (count
+ 1) * sizeof(fsample_m_entry
));
105 n
->samples
= samples
;
110 if(!fsadd_m(n
, filename
, 1, NULL
, 0)) {
118 void fsset_m(fsample_m
*f
, fsample_t pos
, fsample_t value
) {
121 for(i
=0;i
<f
->pos
;i
++) {
124 if((pos
+ 1) % f
->entry
[i
].freq
!= 0)
127 if(f
->entry
[i
].derive
) {
128 v
= (f
->entry
[i
].derive
)(f
, i
, pos
, f
->entry
[i
].tag
);
133 fsset(f
->entry
[i
].f
, pos
/ f
->entry
[i
].freq
, v
);
137 void fsclose_m(fsample_m
*f
) {
140 for(i
=0;i
<f
->pos
;i
++)
141 fsclose(f
->entry
[i
].f
);
145 fsample_t
fsamean(fsample_m
*f
, int entry
, fsample_t pos
, void *tag
) {
147 long samples
= (long)tag
;
149 int rpos
= pos
/ f
->entry
[0].freq
;
153 for(i
=0;i
<samples
;i
++) {
154 if(__fsget_m(f
, 0, rpos
- i
, &t
)) {
158 /* printf("bad :(\n");*/
168 fsample_t
fsapmean(fsample_m
*f
, int entry
, fsample_t pos
, void *tag
) {
170 long samples
= (long)tag
;
172 int rpos
= pos
/ f
->entry
[entry
- 1].freq
;
176 for(i
=0;i
<=samples
;i
++) {
177 if(__fsget(f
->entry
[entry
- 1].f
, rpos
- i
, &t
)) {
181 /*printf("bad :(\n");*/