-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathelog.c
More file actions
78 lines (63 loc) · 1.47 KB
/
elog.c
File metadata and controls
78 lines (63 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// kernel/elog.c
#include "types.h"
#include "riscv.h"
#include "spinlock.h"
#include "defs.h"
#include "elog.h"
extern uint ticks;
extern struct spinlock tickslock;
struct {
struct spinlock lock;
struct elog_entry buf[ELOG_SIZE];
int next_index; // next position to write
int count; // number of valid entries
} elogsys;
static int
getticksafe(void)
{
int t;
acquire(&tickslock);
t = ticks;
release(&tickslock);
return t;
}
void
eloginit(void)
{
initlock(&elogsys.lock, "elog");
elogsys.next_index = 0;
elogsys.count = 0;
}
void
elogadd(int event_type, int sensor_id, int value)
{
acquire(&elogsys.lock);
struct elog_entry *e = &elogsys.buf[elogsys.next_index];
e->timestamp = getticksafe();
e->event_type = event_type;
e->sensor_id = sensor_id;
e->value = value;
elogsys.next_index = (elogsys.next_index + 1) % ELOG_SIZE;
if(elogsys.count < ELOG_SIZE)
elogsys.count++;
release(&elogsys.lock);
}
// Copies logs from oldest -> newest into dst.
// Returns number of entries copied.
int
elogread(struct elog_entry *dst, int max)
{
int i, n, start;
if(dst == 0 || max <= 0)
return 0;
acquire(&elogsys.lock);
n = elogsys.count;
if(max < n)
n = max;
start = (elogsys.next_index - elogsys.count + ELOG_SIZE) % ELOG_SIZE;
for(i = 0; i < n; i++){
dst[i] = elogsys.buf[(start + i) % ELOG_SIZE];
}
release(&elogsys.lock);
return n;
}