forked from AntonKozlov/os226-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.c
188 lines (157 loc) · 3.81 KB
/
apps.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include "sched.h"
#include "usyscall.h"
#include "pool.h"
static int g_retcode;
#define APPS_X(X) \
X(echo) \
X(retcode) \
X(pooltest) \
X(syscalltest) \
X(app) \
X(sched) \
#define DECLARE(X) static int X(int, char *[]);
APPS_X(DECLARE)
#undef DECLARE
static const struct app {
const char *name;
int (*fn)(int, char *[]);
} app_list[] = {
#define ELEM(X) { # X, X },
APPS_X(ELEM)
#undef ELEM
};
static long reftime(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
static int os_printf(const char *fmt, ...) {
char buf[128];
va_list ap;
va_start(ap, fmt);
int ret = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
return os_write(1, buf, ret);
}
static int echo(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i) {
printf("%s%c", argv[i], i == argc - 1 ? '\n' : ' ');
}
fflush(stdout);
return argc - 1;
}
static int retcode(int argc, char *argv[]) {
printf("%d\n", g_retcode);
fflush(stdout);
return 0;
}
struct app_ctx {
int cnt;
} ctxarray[16];
struct pool ctxpool = POOL_INITIALIZER_ARRAY(ctxarray);
static void print(struct app_ctx *ctx, const char *msg) {
static long refstart;
if (!refstart) {
refstart = reftime();
}
printf("%16s id %d cnt %d time %ld reftime %ld ctx %p\n",
msg, 1 + ctx - ctxarray, ctx->cnt, sched_gettime(), reftime() - refstart, &ctx);
fflush(stdout);
}
static void app_burn(void *_ctx) {
struct app_ctx *ctx = _ctx;
while (1) {
print(ctx, "burn");
for (volatile int i = 10000000 * ctx->cnt; 0 < i; --i) {
}
}
}
static void app_preempt_sleep(void *_ctx) {
struct app_ctx *ctx = _ctx;
int cnt = ctx->cnt % 1000;
for (int i = 0; i < cnt; ++i) {
print(ctx, "sleep");
sched_sleep(ctx->cnt - cnt);
}
}
static int app(int argc, char* argv[]) {
int entry_id = atoi(argv[1]) - 1;
struct app_ctx *ctx = pool_alloc(&ctxpool);
ctx->cnt = atoi(argv[2]);
void (*entries[])(void*) = { app_burn, app_preempt_sleep };
sched_new(entries[entry_id], ctx, atoi(argv[3]));
}
static int sched(int argc, char* argv[]) {
sched_run(atoi(argv[1]));
}
static int exec(int argc, char *argv[]) {
const struct app *app = NULL;
for (int i = 0; i < ARRAY_SIZE(app_list); ++i) {
if (!strcmp(argv[0], app_list[i].name)) {
app = &app_list[i];
break;
}
}
if (!app) {
printf("Unknown command\n");
return 1;
}
g_retcode = app->fn(argc, argv);
return g_retcode;
}
static int pooltest(int argc, char *argv[]) {
struct obj {
void *field1;
void *field2;
};
static struct obj objmem[4];
static struct pool objpool = POOL_INITIALIZER_ARRAY(objmem);
if (!strcmp(argv[1], "alloc")) {
struct obj *o = pool_alloc(&objpool);
printf("alloc %d\n", o ? (o - objmem) : -1);
return 0;
} else if (!strcmp(argv[1], "free")) {
int iobj = atoi(argv[2]);
printf("free %d\n", iobj);
pool_free(&objpool, objmem + iobj);
return 0;
}
}
static int syscalltest(int argc, char *argv[]) {
int r = os_printf("%s\n", argv[1]);
return r - 1;
}
int shell(int argc, char *argv[]) {
char line[256];
while (fgets(line, sizeof(line), stdin)) {
const char *comsep = "\n;";
char *stcmd;
char *cmd = strtok_r(line, comsep, &stcmd);
while (cmd) {
const char *argsep = " \t";
char *starg;
char *arg = strtok_r(cmd, argsep, &starg);
char *argv[256];
int argc = 0;
while (arg && arg[0] != '#') {
argv[argc++] = arg;
arg = strtok_r(NULL, argsep, &starg);
}
argv[argc] = NULL;
if (!argc) {
break;
}
exec(argc, argv);
cmd = strtok_r(NULL, comsep, &stcmd);
}
}
return 0;
}