centralized performance resources

This commit is contained in:
Barrett Ruth 2026-01-10 12:21:52 -05:00
commit 50b15a1522
63 changed files with 328466 additions and 0 deletions

31
ocw/1/c-primer/Makefile Normal file
View file

@ -0,0 +1,31 @@
CC := clang
ifeq ($(DEBUG),1)
CFLAGS := -Wall -O0 -g
else
CFLAGS := -Wall -O1 -DNDEBUG
endif
LDFLAGS := -lrt -flto -fuse-ld=gold
all: sizes pointer swap
sizes.o: sizes.c
$(CC) $(CFLAGS) -c sizes.c
sizes: sizes.o
$(CC) -o sizes sizes.o $(LDFLAGS)
pointer.o: pointer.c
$(CC) $(CFLAGS) -c pointer.c
pointer: pointer.o
$(CC) -o pointer pointer.o $(LDFLAGS)
swap.o: swap.c
$(CC) $(CFLAGS) -c swap.c
swap: swap.o
$(CC) -o swap swap.o $(LDFLAGS)
clean:
rm -f sizes pointer swap *.o *.gcda *.gcno *.gcov perf.data */perf.data cachegrind.out.*

47
ocw/1/c-primer/pointer.c Normal file
View file

@ -0,0 +1,47 @@
// Copyright (c) 2012 MIT License by 6.172 Staff
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char * argv[]) { // What is the type of argv?
int i = 5;
// The & operator here gets the address of i and stores it into pi
int * pi = &i;
// The * operator here dereferences pi and stores the value -- 5 --
// into j.
int j = *pi;
char c[] = "6.172";
char * pc = c; // Valid assignment: c acts like a pointer to c[0] here.
char d = *pc;
printf("char d = %c\n", d); // What does this print?
// compound types are read right to left in C.
// pcp is a pointer to a pointer to a char, meaning that
// pcp stores the address of a char pointer.
char ** pcp;
pcp = argv; // Why is this assignment valid?
const char * pcc = c; // pcc is a pointer to char constant
char const * pcc2 = c; // What is the type of pcc2?
// For each of the following, why is the assignment:
*pcc = '7'; // invalid?
pcc = *pcp; // valid?
pcc = argv[0]; // valid?
char * const cp = c; // cp is a const pointer to char
// For each of the following, why is the assignment:
cp = *pcp; // invalid?
cp = *argv; // invalid?
*cp = '!'; // valid?
const char * const cpc = c; // cpc is a const pointer to char const
// For each of the following, why is the assignment:
cpc = *pcp; // invalid?
cpc = argv[0]; // invalid?
*cpc = '@'; // invalid?
return 0;
}

View file

@ -0,0 +1,21 @@
// Copyright (c) 2012 MIT License by 6.172 Staff
// All occurences of ONE will be replaced by 1.
#define ONE 1
// Macros can also behave similar to inline functions.
// Note that parentheses around arguments are required to preserve order of
// operations. Otherwise, you can introduce bugs when substitution happens
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int c = ONE, d = ONE + 5;
int e = MIN(c, d);
#ifndef NDEBUG
// This code will be compiled only when
// the macro NDEBUG is not defined.
// Recall that if clang is passed -DNDEBUG on the command line,
// then NDEBUG will be defined.
if (something) {}
#endif

14
ocw/1/c-primer/sizes.c Normal file
View file

@ -0,0 +1,14 @@
// Copyright (c) 2012 MIT License by 6.172 Staff
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void subprocedure(int x, int y) {
int z = x + y;
}
int main() {
int x = 3, y = 5;
subprocedure(x, y);
}

476
ocw/1/c-primer/sizes.s Normal file
View file

@ -0,0 +1,476 @@
.file "sizes.c"
.text
.Ltext0:
.file 0 "/home/frozen/dev/performance-engineering-ocw/1/c-primer" "sizes.c"
.section .rodata
.LC0:
.string "size of int : %zu bytes \n"
.LC1:
.string "size of you : %zu bytes \n"
.text
.globl main
.type main, @function
main:
.LFB6:
.file 1 "sizes.c"
.loc 1 9 12
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $48, %rsp
.loc 1 9 12
movq %fs:40, %rax
movq %rax, -8(%rbp)
xorl %eax, %eax
.loc 1 18 3
leaq .LC0(%rip), %rax
movl $4, %esi
movq %rax, %rdi
movl $0, %eax
call printf@PLT
.loc 1 35 10
movl $12345, -40(%rbp)
.loc 1 36 12
movl $4, -36(%rbp)
.loc 1 43 3
leaq .LC1(%rip), %rax
movl $8, %esi
movq %rax, %rdi
movl $0, %eax
call printf@PLT
.loc 1 45 10
movl $0, %eax
.loc 1 46 1
movq -8(%rbp), %rdx
subq %fs:40, %rdx
je .L3
call __stack_chk_fail@PLT
.L3:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE6:
.size main, .-main
.Letext0:
.file 2 "/usr/include/stdio.h"
.section .debug_info,"",@progbits
.Ldebug_info0:
.long 0x118
.value 0x5
.byte 0x1
.byte 0x8
.long .Ldebug_abbrev0
.uleb128 0x3
.long .LASF13
.byte 0x1d
.byte 0x3
.long 0x31647
.long .LASF0
.long .LASF1
.quad .Ltext0
.quad .Letext0-.Ltext0
.long .Ldebug_line0
.uleb128 0x1
.byte 0x8
.byte 0x7
.long .LASF2
.uleb128 0x1
.byte 0x4
.byte 0x7
.long .LASF3
.uleb128 0x1
.byte 0x1
.byte 0x8
.long .LASF4
.uleb128 0x1
.byte 0x2
.byte 0x7
.long .LASF5
.uleb128 0x1
.byte 0x1
.byte 0x6
.long .LASF6
.uleb128 0x1
.byte 0x2
.byte 0x5
.long .LASF7
.uleb128 0x4
.byte 0x4
.byte 0x5
.string "int"
.uleb128 0x1
.byte 0x8
.byte 0x5
.long .LASF8
.uleb128 0x1
.byte 0x1
.byte 0x6
.long .LASF9
.uleb128 0x5
.long 0x6b
.uleb128 0x6
.byte 0x8
.long 0x72
.uleb128 0x1
.byte 0x8
.byte 0x5
.long .LASF10
.uleb128 0x1
.byte 0x8
.byte 0x7
.long .LASF11
.uleb128 0x7
.long .LASF14
.byte 0x2
.value 0x16b
.byte 0xc
.long 0x5d
.long 0xa3
.uleb128 0x8
.long 0x77
.uleb128 0x9
.byte 0
.uleb128 0xa
.long .LASF15
.byte 0x1
.byte 0x9
.byte 0x5
.long 0x5d
.quad .LFB6
.quad .LFE6-.LFB6
.uleb128 0x1
.byte 0x9c
.long 0x10f
.uleb128 0xb
.byte 0x8
.byte 0x1
.byte 0x1d
.byte 0xb
.long 0xe8
.uleb128 0xc
.string "id"
.byte 0x1
.byte 0x1e
.byte 0x9
.long 0x5d
.byte 0
.uleb128 0xd
.long .LASF12
.byte 0x1
.byte 0x1f
.byte 0x9
.long 0x5d
.byte 0x4
.byte 0
.uleb128 0xe
.long .LASF16
.byte 0x1
.byte 0x20
.byte 0x5
.long 0xc5
.uleb128 0x2
.string "you"
.byte 0x22
.byte 0xb
.long 0xe8
.uleb128 0x2
.byte 0x91
.sleb128 -56
.uleb128 0x2
.string "x"
.byte 0x28
.byte 0x7
.long 0x10f
.uleb128 0x2
.byte 0x91
.sleb128 -48
.byte 0
.uleb128 0xf
.long 0x5d
.uleb128 0x10
.long 0x33
.byte 0x4
.byte 0
.byte 0
.section .debug_abbrev,"",@progbits
.Ldebug_abbrev0:
.uleb128 0x1
.uleb128 0x24
.byte 0
.uleb128 0xb
.uleb128 0xb
.uleb128 0x3e
.uleb128 0xb
.uleb128 0x3
.uleb128 0xe
.byte 0
.byte 0
.uleb128 0x2
.uleb128 0x34
.byte 0
.uleb128 0x3
.uleb128 0x8
.uleb128 0x3a
.uleb128 0x21
.sleb128 1
.uleb128 0x3b
.uleb128 0xb
.uleb128 0x39
.uleb128 0xb
.uleb128 0x49
.uleb128 0x13
.uleb128 0x2
.uleb128 0x18
.byte 0
.byte 0
.uleb128 0x3
.uleb128 0x11
.byte 0x1
.uleb128 0x25
.uleb128 0xe
.uleb128 0x13
.uleb128 0xb
.uleb128 0x90
.uleb128 0xb
.uleb128 0x91
.uleb128 0x6
.uleb128 0x3
.uleb128 0x1f
.uleb128 0x1b
.uleb128 0x1f
.uleb128 0x11
.uleb128 0x1
.uleb128 0x12
.uleb128 0x7
.uleb128 0x10
.uleb128 0x17
.byte 0
.byte 0
.uleb128 0x4
.uleb128 0x24
.byte 0
.uleb128 0xb
.uleb128 0xb
.uleb128 0x3e
.uleb128 0xb
.uleb128 0x3
.uleb128 0x8
.byte 0
.byte 0
.uleb128 0x5
.uleb128 0x26
.byte 0
.uleb128 0x49
.uleb128 0x13
.byte 0
.byte 0
.uleb128 0x6
.uleb128 0xf
.byte 0
.uleb128 0xb
.uleb128 0xb
.uleb128 0x49
.uleb128 0x13
.byte 0
.byte 0
.uleb128 0x7
.uleb128 0x2e
.byte 0x1
.uleb128 0x3f
.uleb128 0x19
.uleb128 0x3
.uleb128 0xe
.uleb128 0x3a
.uleb128 0xb
.uleb128 0x3b
.uleb128 0x5
.uleb128 0x39
.uleb128 0xb
.uleb128 0x27
.uleb128 0x19
.uleb128 0x49
.uleb128 0x13
.uleb128 0x3c
.uleb128 0x19
.uleb128 0x1
.uleb128 0x13
.byte 0
.byte 0
.uleb128 0x8
.uleb128 0x5
.byte 0
.uleb128 0x49
.uleb128 0x13
.byte 0
.byte 0
.uleb128 0x9
.uleb128 0x18
.byte 0
.byte 0
.byte 0
.uleb128 0xa
.uleb128 0x2e
.byte 0x1
.uleb128 0x3f
.uleb128 0x19
.uleb128 0x3
.uleb128 0xe
.uleb128 0x3a
.uleb128 0xb
.uleb128 0x3b
.uleb128 0xb
.uleb128 0x39
.uleb128 0xb
.uleb128 0x27
.uleb128 0x19
.uleb128 0x49
.uleb128 0x13
.uleb128 0x11
.uleb128 0x1
.uleb128 0x12
.uleb128 0x7
.uleb128 0x40
.uleb128 0x18
.uleb128 0x7c
.uleb128 0x19
.uleb128 0x1
.uleb128 0x13
.byte 0
.byte 0
.uleb128 0xb
.uleb128 0x13
.byte 0x1
.uleb128 0xb
.uleb128 0xb
.uleb128 0x3a
.uleb128 0xb
.uleb128 0x3b
.uleb128 0xb
.uleb128 0x39
.uleb128 0xb
.uleb128 0x1
.uleb128 0x13
.byte 0
.byte 0
.uleb128 0xc
.uleb128 0xd
.byte 0
.uleb128 0x3
.uleb128 0x8
.uleb128 0x3a
.uleb128 0xb
.uleb128 0x3b
.uleb128 0xb
.uleb128 0x39
.uleb128 0xb
.uleb128 0x49
.uleb128 0x13
.uleb128 0x38
.uleb128 0xb
.byte 0
.byte 0
.uleb128 0xd
.uleb128 0xd
.byte 0
.uleb128 0x3
.uleb128 0xe
.uleb128 0x3a
.uleb128 0xb
.uleb128 0x3b
.uleb128 0xb
.uleb128 0x39
.uleb128 0xb
.uleb128 0x49
.uleb128 0x13
.uleb128 0x38
.uleb128 0xb
.byte 0
.byte 0
.uleb128 0xe
.uleb128 0x16
.byte 0
.uleb128 0x3
.uleb128 0xe
.uleb128 0x3a
.uleb128 0xb
.uleb128 0x3b
.uleb128 0xb
.uleb128 0x39
.uleb128 0xb
.uleb128 0x49
.uleb128 0x13
.byte 0
.byte 0
.uleb128 0xf
.uleb128 0x1
.byte 0x1
.uleb128 0x49
.uleb128 0x13
.byte 0
.byte 0
.uleb128 0x10
.uleb128 0x21
.byte 0
.uleb128 0x49
.uleb128 0x13
.uleb128 0x2f
.uleb128 0xb
.byte 0
.byte 0
.byte 0
.section .debug_aranges,"",@progbits
.long 0x2c
.value 0x2
.long .Ldebug_info0
.byte 0x8
.byte 0
.value 0
.value 0
.quad .Ltext0
.quad .Letext0-.Ltext0
.quad 0
.quad 0
.section .debug_line,"",@progbits
.Ldebug_line0:
.section .debug_str,"MS",@progbits,1
.LASF10:
.string "long long int"
.LASF3:
.string "unsigned int"
.LASF15:
.string "main"
.LASF13:
.string "GNU C23 15.1.1 20250425 -mtune=generic -march=x86-64 -g -O0"
.LASF2:
.string "long unsigned int"
.LASF11:
.string "long long unsigned int"
.LASF4:
.string "unsigned char"
.LASF9:
.string "char"
.LASF8:
.string "long int"
.LASF16:
.string "student"
.LASF12:
.string "year"
.LASF5:
.string "short unsigned int"
.LASF14:
.string "printf"
.LASF7:
.string "short int"
.LASF6:
.string "signed char"
.section .debug_line_str,"MS",@progbits,1
.LASF0:
.string "sizes.c"
.LASF1:
.string "/home/frozen/dev/performance-engineering-ocw/1/c-primer"
.ident "GCC: (GNU) 15.1.1 20250425"
.section .note.GNU-stack,"",@progbits

21
ocw/1/c-primer/swap.c Normal file
View file

@ -0,0 +1,21 @@
// Copyright (c) 2012 MIT License by 6.172 Staff
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void swap(int i, int j) {
int temp = i;
i = j;
j = temp;
}
int main() {
int k = 1;
int m = 2;
swap(k, m);
// What does this print?
printf("k = %d, m = %d\n", k, m);
return 0;
}

111
ocw/1/c-primer/verifier.py Normal file
View file

@ -0,0 +1,111 @@
#!/usr/bin/python
import subprocess, os, sys, re
def exitWithError(error):
print error
sys.exit(1)
def runAndReadOutput(args):
if args is str:
args = [args]
try:
return subprocess.check_output(args)
except subprocess.CalledProcessError as e:
exitWithError("ERROR: runtime error with %s" % str(args))
def run(path): runAndReadOutput(path)
def runAndCheckSizes():
output = runAndReadOutput("./sizes")
expected_output_format = "size of %s : %d bytes"
lines = set([x.replace(" ", "") for x in output.strip().lower().split('\n')])
types = [
( "int", 4 ),
( "short", 2 ),
( "long", 8 ),
( "char", 1 ),
( "float", 4 ),
( "double", 8 ),
( "unsigned int", 4 ),
( "long long", 8 ),
( "uint8_t", 1 ),
( "uint16_t", 2 ),
( "uint32_t", 4 ),
( "uint64_t", 8 ),
( "uint_fast8_t", 1 ),
( "uint_fast16_t", 8 ),
( "uintmax_t", 8 ),
( "intmax_t", 8 ),
( "__int128", 16 ),
( "uint32_t", 4 ),
( "uint64_t", 8 ),
( "student", 8 ),
( "x", 20),
( "int*", 8 ),
( "short*", 8 ),
( "long*", 8 ),
( "char*", 8 ),
( "float*", 8 ),
( "double*", 8 ),
( "unsigned int*", 8 ),
( "long long*", 8 ),
( "uint8_t*", 8 ),
( "uint16_t*", 8 ),
( "uint32_t*", 8 ),
( "uint64_t*", 8 ),
( "uint_fast8_t*", 8 ),
( "uint_fast16_t*", 8 ),
( "uintmax_t*", 8 ),
( "intmax_t*", 8 ),
( "__int128*", 8 ),
( "uint32_t*", 8 ),
( "uint64_t*", 8 ),
( "student*", 8 ),
( "&x", 8)
]
for typ in types:
print (expected_output_format % typ)
if (expected_output_format % typ).replace(" ", "") not in lines:
exitWithError("ERROR: couldn't find type %s (or it has the incorrect value) in sizes output" % typ[0])
def runAndCheckSwap():
expected_output = "k = 2, m = 1\n"
output = runAndReadOutput("./swap")
if output != expected_output:
exitWithError('ERROR: actual output: "%s", expected "%s"' % (output, expected_output))
def build(make_arg, filename):
print "\nRunning make %s ... " % make_arg
run(["make", filename])
print "Ok!"
print "\nChecking that %s was built ... " % filename
if not os.path.isfile(filename):
exitWithError("ERROR: %s binary missing, did you rename it?" % filename)
print "Ok!"
print "Running verifying script ... "
print "\nChecking that the Makefile exists ... "
if not os.path.isfile('Makefile'):
exitWithError('ERROR: Makefile does not exist.')
print "Good!"
build("sizes", "sizes")
print "Checking output of sizes ... "
runAndCheckSizes()
print "Ok!"
build("pointer", "pointer")
run("./pointer") # Run pointer as a sanity check, but there's no output to check
build("swap", "swap")
print "Checking output of swap ... "
runAndCheckSwap()
print "Ok!"
print "LGTM"