Skip to content

Commit cdc40ad

Browse files
committed
Kernel 1 lecture code
1 parent 41b882f commit cdc40ad

13 files changed

+525
-0
lines changed

kernel1/.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
allocate
2+
allocaterandomtouch
3+
allocatetouch
4+
callocate
5+
data
6+
map
7+
maprandomtouch
8+
maptouch
9+
mapwrite
10+
read
11+
readrandomtouch
12+
readtouch

kernel1/GNUmakefile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
PROGRAMS = allocate allocatetouch allocaterandomtouch callocate \
2+
map maptouch maprandomtouch mapwrite \
3+
read readtouch readrandomtouch
4+
all: $(PROGRAMS)
5+
6+
include ../common/rules.mk
7+
8+
%.o: %.c $(BUILDSTAMP)
9+
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPCFLAGS) $(O) -o $@ -c $<
10+
11+
$(PROGRAMS): %: %.o
12+
$(CC) $(CFLAGS) $(O) -o $@ $^ -lm
13+
14+
data: GNUmakefile
15+
yes 77777777777777777777777777777777777777777 | tr -d '\n' | head -c 1073741824 > data
16+
17+
flush-buffer-cache:
18+
sudo sh -c "sync; echo 1 > /proc/sys/vm/drop_caches"
19+
20+
clean:
21+
rm -f *.o $(PROGRAMS) data
22+
rm -rf $(DEPSDIR) *.dSYM
23+
24+
.PHONY: all clean clear-buffer-cache

kernel1/allocate.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
6+
int main(int argc, char* argv[]) {
7+
size_t sz = 4096;
8+
int opt;
9+
while ((opt = getopt(argc, argv, "s:")) != -1)
10+
if (opt == 's')
11+
sz = strtoul(optarg, NULL, 0);
12+
else {
13+
fprintf(stderr, "Usage: ./allocate [-s SIZE]\n");
14+
exit(EXIT_FAILURE);
15+
}
16+
17+
void* ptr = malloc(sz);
18+
printf("allocated %zu bytes at %p\n", sz, ptr);
19+
20+
sleep(100);
21+
}

kernel1/allocaterandomtouch.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
6+
int main(int argc, char* argv[]) {
7+
size_t sz = 4096;
8+
useconds_t delay = 0;
9+
int opt;
10+
while ((opt = getopt(argc, argv, "s:d:")) != -1)
11+
if (opt == 's')
12+
sz = strtoul(optarg, NULL, 0);
13+
else if (opt == 'd')
14+
delay = (useconds_t) (strtod(optarg, NULL) * 1000000);
15+
else {
16+
fprintf(stderr, "Usage: ./allocaterandomtouch [-s SIZE] [-d DELAY]\n");
17+
exit(EXIT_FAILURE);
18+
}
19+
20+
char* ptr = (char*) malloc(sz);
21+
printf("allocated %zu bytes at %p\n", sz, ptr);
22+
23+
size_t npages = sz / 4096;
24+
for (size_t pagecount = 0; pagecount < npages; ++pagecount) {
25+
size_t pos = random() % npages;
26+
ptr[pos * 4096] = 1;
27+
if (delay > 0) {
28+
usleep(delay);
29+
if (pagecount && pagecount % 1000 == 0)
30+
printf("touched %zu pages\n", pagecount);
31+
}
32+
}
33+
34+
if (delay > 0)
35+
sleep(100);
36+
}

kernel1/allocatetouch.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
6+
int main(int argc, char* argv[]) {
7+
size_t sz = 4096;
8+
useconds_t delay = 0;
9+
int opt;
10+
while ((opt = getopt(argc, argv, "s:d:")) != -1)
11+
if (opt == 's')
12+
sz = strtoul(optarg, NULL, 0);
13+
else if (opt == 'd')
14+
delay = (useconds_t) (strtod(optarg, NULL) * 1000000);
15+
else {
16+
fprintf(stderr, "Usage: ./allocatetouch [-s SIZE] [-d DELAY]\n");
17+
exit(EXIT_FAILURE);
18+
}
19+
20+
char* ptr = (char*) malloc(sz);
21+
printf("allocated %zu bytes at %p\n", sz, ptr);
22+
23+
for (size_t pagecount = 0; pagecount * 4096 < sz; ++pagecount) {
24+
ptr[pagecount * 4096] = 1;
25+
if (delay > 0) {
26+
usleep(delay);
27+
if (pagecount && pagecount % 1000 == 0)
28+
printf("touched %zu pages\n", pagecount);
29+
}
30+
}
31+
32+
if (delay > 0)
33+
sleep(100);
34+
}

kernel1/callocate.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
6+
int main(int argc, char* argv[]) {
7+
size_t sz = 4096;
8+
if (argc > 1)
9+
sz = strtoul(argv[1], NULL, 0);
10+
11+
void* ptr = calloc(sz, 1);
12+
printf("allocated %zu bytes at %p\n", sz, ptr);
13+
14+
sleep(100);
15+
}

kernel1/map.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
#include <fcntl.h>
6+
#include <errno.h>
7+
#include <sys/mman.h>
8+
#include <sys/stat.h>
9+
10+
int main(int argc, char* argv[]) {
11+
const char* filename = "data";
12+
size_t sz = (size_t) -1;
13+
int quit = 0, opt;
14+
while ((opt = getopt(argc, argv, "f:s:q")) != -1)
15+
if (opt == 'f')
16+
filename = optarg;
17+
else if (opt == 's')
18+
sz = strtoul(optarg, NULL, 0);
19+
else if (opt == 'q')
20+
quit = 1;
21+
else {
22+
fprintf(stderr, "Usage: ./map [-f FILE] [-s SIZE] [-q]\n");
23+
exit(EXIT_FAILURE);
24+
}
25+
26+
int fd = open(filename, O_RDONLY);
27+
if (fd == -1) {
28+
fprintf(stderr, "%s: %s\n", filename, strerror(errno));
29+
exit(EXIT_FAILURE);
30+
}
31+
32+
if (sz == (size_t) -1) {
33+
struct stat s;
34+
if (fstat(fd, &s) == 0)
35+
sz = s.st_size;
36+
}
37+
38+
void* ptr = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
39+
printf("mapped %zu bytes at %p\n", sz, ptr);
40+
41+
if (!quit)
42+
sleep(100);
43+
}

kernel1/maprandomtouch.c

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
#include <fcntl.h>
6+
#include <errno.h>
7+
#include <sys/mman.h>
8+
#include <sys/stat.h>
9+
10+
int main(int argc, char* argv[]) {
11+
const char* filename = "data";
12+
size_t sz = (size_t) -1;
13+
useconds_t delay = 0;
14+
int opt;
15+
while ((opt = getopt(argc, argv, "f:s:d:")) != -1)
16+
if (opt == 'f')
17+
filename = optarg;
18+
else if (opt == 's')
19+
sz = strtoul(optarg, NULL, 0);
20+
else if (opt == 'd')
21+
delay = (useconds_t) (strtod(optarg, NULL) * 1000000);
22+
else {
23+
fprintf(stderr, "Usage: ./maprandomtouch [-f FILE] [-s SIZE] [-d DELAY]\n");
24+
exit(EXIT_FAILURE);
25+
}
26+
27+
int fd = open(filename, O_RDONLY);
28+
if (fd == -1) {
29+
fprintf(stderr, "%s: %s\n", filename, strerror(errno));
30+
exit(EXIT_FAILURE);
31+
}
32+
33+
if (sz == (size_t) -1) {
34+
struct stat s;
35+
if (fstat(fd, &s) == 0)
36+
sz = s.st_size;
37+
}
38+
39+
unsigned char* ptr = (unsigned char*)
40+
mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
41+
printf("mapped %zu bytes at %p\n", sz, ptr);
42+
43+
size_t npages = sz / 4096;
44+
size_t checksum = 0;
45+
for (size_t pagecount = 0; pagecount < npages; ++pagecount) {
46+
size_t pos = random() % npages;
47+
checksum += ptr[pos * 4096];
48+
if (delay > 0) {
49+
usleep(delay);
50+
if (pagecount && pagecount % 1000 == 0)
51+
printf("touched %zu pages\n", pagecount);
52+
}
53+
}
54+
55+
printf("checksum %zu\n", checksum);
56+
if (delay > 0)
57+
sleep(100);
58+
}

kernel1/maptouch.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
#include <fcntl.h>
6+
#include <errno.h>
7+
#include <sys/mman.h>
8+
#include <sys/stat.h>
9+
10+
int main(int argc, char* argv[]) {
11+
const char* filename = "data";
12+
size_t sz = (size_t) -1;
13+
useconds_t delay = 0;
14+
int opt;
15+
while ((opt = getopt(argc, argv, "f:s:d:")) != -1)
16+
if (opt == 'f')
17+
filename = optarg;
18+
else if (opt == 's')
19+
sz = strtoul(optarg, NULL, 0);
20+
else if (opt == 'd')
21+
delay = (useconds_t) (strtod(optarg, NULL) * 1000000);
22+
else {
23+
fprintf(stderr, "Usage: ./maptouch [-f FILE] [-s SIZE] [-d DELAY]\n");
24+
exit(EXIT_FAILURE);
25+
}
26+
27+
int fd = open(filename, O_RDONLY);
28+
if (fd == -1) {
29+
fprintf(stderr, "%s: %s\n", filename, strerror(errno));
30+
exit(EXIT_FAILURE);
31+
}
32+
33+
if (sz == (size_t) -1) {
34+
struct stat s;
35+
if (fstat(fd, &s) == 0)
36+
sz = s.st_size;
37+
}
38+
39+
unsigned char* ptr = (unsigned char*)
40+
mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
41+
printf("mapped %zu bytes at %p\n", sz, ptr);
42+
43+
size_t npages = sz / 4096;
44+
size_t checksum = 0;
45+
for (size_t pagecount = 0; pagecount < npages; ++pagecount) {
46+
checksum += ptr[pagecount * 4096];
47+
if (delay > 0) {
48+
usleep(delay);
49+
if (pagecount && pagecount % 1000 == 0)
50+
printf("touched %zu pages\n", pagecount);
51+
}
52+
}
53+
54+
printf("checksum %zu\n", checksum);
55+
if (delay > 0)
56+
sleep(100);
57+
}

kernel1/mapwrite.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
#include <fcntl.h>
6+
#include <errno.h>
7+
#include <sys/mman.h>
8+
#include <sys/stat.h>
9+
10+
int main(int argc, char* argv[]) {
11+
const char* filename = "data";
12+
size_t sz = (size_t) -1;
13+
useconds_t delay = 0;
14+
int opt;
15+
while ((opt = getopt(argc, argv, "f:s:d:")) != -1)
16+
if (opt == 'f')
17+
filename = optarg;
18+
else if (opt == 's')
19+
sz = strtoul(optarg, NULL, 0);
20+
else if (opt == 'd')
21+
delay = (useconds_t) (strtod(optarg, NULL) * 1000000);
22+
else {
23+
fprintf(stderr, "Usage: ./maptouch [-f FILE] [-s SIZE] [-d DELAY]\n");
24+
exit(EXIT_FAILURE);
25+
}
26+
27+
int fd = open(filename, O_RDWR);
28+
if (fd == -1) {
29+
fprintf(stderr, "%s: %s\n", filename, strerror(errno));
30+
exit(EXIT_FAILURE);
31+
}
32+
33+
if (sz == (size_t) -1) {
34+
struct stat s;
35+
if (fstat(fd, &s) == 0)
36+
sz = s.st_size;
37+
}
38+
39+
unsigned char* ptr = (unsigned char*)
40+
mmap(NULL, sz, PROT_WRITE, MAP_SHARED, fd, 0);
41+
printf("mapped %zu bytes at %p\n", sz, ptr);
42+
43+
for (size_t pagecount = 0; pagecount * 4096 < sz; ++pagecount) {
44+
ptr[pagecount * 4096] = '8';
45+
if (delay > 0) {
46+
usleep(delay);
47+
if (pagecount && pagecount % 1000 == 0)
48+
printf("touched %zu pages\n", pagecount);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)