Skip to content

Commit a29cc65

Browse files
committed
Make egg standlone, and improve tty boardcasting
1 parent 08ea0fd commit a29cc65

3 files changed

Lines changed: 101 additions & 53 deletions

File tree

kernel/sched/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ endif
3030
#
3131
obj-y += core.o
3232
obj-y += easy.o
33+
obj-y += egg.o
3334
obj-y += build_policy.o
3435
obj-y += build_utility.o

kernel/sched/easy.c

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#include <linux/sched/clock.h>
21
#include <linux/sched/cputime.h>
3-
#include <linux/timekeeper_internal.h>
4-
#include <linux/tty.h>
5-
#include <linux/sched/isolation.h>
6-
#include <linux/sched/nohz.h>
72
#include "sched.h"
3+
4+
void egg(void);
5+
void egg2(struct task_struct *p);
6+
87
struct kthread {
98
unsigned long flags;
109
unsigned int cpu;
@@ -30,48 +29,6 @@ typedef struct {
3029

3130
easy_sched_struct_def easy_cpu_contexts[128] = {0};
3231

33-
static const char *unix_days[] = {"Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"};
34-
static const char *prof_words[] = {"\033[91mYou can't just delete that!\033[0m", "\033[93mYour math is too poor!\033[0m", "\033[95mWithout the formula, it's meaningless!\033[0m", "\033[91mGo back and study linear algebra!\033[0m", "\033[96mThis is not rigorous.\033[0m", "\033[96mWe need publish papers!\033[0m", "\033[93mIntuition won't work here!\033[0m", "\033[95mThis is not scientific research!\033[0m", "\033[91mYou are running blind code!\033[0m"};
35-
static __inline void egg(void)
36-
{
37-
static uint8_t shown = 0;
38-
if (shown) return;
39-
pr_alert("easy_sched: From Beijing to Hiroshima, Professors kept saying the same thing:\n");
40-
pr_alert("easy_sched: 'Your math is too poor!'\n");
41-
pr_alert("easy_sched: But every time they said it, I had already deleted their math-heavy bullshit,\n");
42-
pr_alert("easy_sched: and the system still ran — faster, cleaner, simpler.\n");
43-
if (global_timekeeper.xtime_sec)
44-
pr_alert("!!! Happy %s !!!\n", unix_days[(global_timekeeper.xtime_sec/86400)%7]);
45-
else
46-
pr_alert("!!! Happy ???Day, time unknown 233 !!!\n");
47-
shown = 1;
48-
}
49-
static __inline void egg2(struct task_struct *p)
50-
{
51-
static uint64_t last_yield = 0;
52-
if (global_timekeeper.ktime_sec-last_yield<30) return;
53-
if (p->utime%255 < 233) return;
54-
if (p->signal && p->signal->tty)
55-
{
56-
char msg_buf[256] = {0}; uint8_t msg_len = 0;
57-
msg_len = sprintf(msg_buf, "\033[1;33mWarning from Prof with %s(PID=%lld):\033[0m %s\n", p->comm, p->pid, prof_words[get_cycles()%(sizeof(prof_words)/sizeof(char *))]);
58-
switch(p->signal->tty->driver->type) {
59-
case TTY_DRIVER_TYPE_SERIAL:
60-
uart_write233(p->signal->tty, msg_buf, msg_len, 0); // no flush, prevent hardware corruption
61-
break;
62-
case TTY_DRIVER_TYPE_CONSOLE:
63-
case TTY_DRIVER_TYPE_PTY:
64-
p->signal->tty->driver->ops->write(p->signal->tty, msg_buf, msg_len);
65-
break;
66-
default:
67-
pr_alert("egg2: unknown %s type %d\n", p->signal->tty->name, p->signal->tty->driver->type);
68-
break;
69-
}
70-
}
71-
else pr_alert("\033[1;31mProf was ANGRY with %s(PID=%lld):\033[0m %s\n", p->comm, p->pid, prof_words[get_cycles()%(sizeof(prof_words)/sizeof(char *))]);
72-
last_yield = global_timekeeper.ktime_sec;
73-
}
74-
7532
void easy_sched_init(easy_sched_struct_def *easy_context)
7633
{
7734
easy_context->tasks = kmalloc_array(EASY_MAX_TASKS, sizeof(void *), GFP_KERNEL);
@@ -183,9 +140,6 @@ static void switched_from_easy(struct rq *rq, struct task_struct *p) { }
183140
static void switched_to_easy(struct rq *rq, struct task_struct *p) { }
184141
static unsigned int get_rr_interval_easy(struct rq *rq, struct task_struct *task) { return HZ; }
185142
static void update_curr_easy(struct rq *rq) { }
186-
#ifdef CONFIG_SCHED_CORE
187-
static int task_is_throttled_easy(struct task_struct *p, int cpu) {return 0;}
188-
#endif
189143

190144
//const struct sched_class easy_sched_class __section("__sched_class") =
191145
DEFINE_SCHED_CLASS(easy) =
@@ -215,7 +169,4 @@ DEFINE_SCHED_CLASS(easy) =
215169
.switched_to = switched_to_easy,
216170
.get_rr_interval = get_rr_interval_easy,
217171
.update_curr = update_curr_easy,
218-
#ifdef CONFIG_SCHED_CORE
219-
.task_is_throttled = task_is_throttled_easy,
220-
#endif
221172
};

kernel/sched/egg.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <linux/timekeeper_internal.h>
2+
#include <linux/tty.h>
3+
#include <linux/console.h>
4+
5+
static const char *unix_days[] = {"Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"};
6+
static const char *color_prefixes[] = {
7+
"\033[1;31m", // Red
8+
"\033[1;32m", // Green
9+
"\033[1;33m", // Yellow
10+
"\033[1;34m", // Blue
11+
"\033[1;35m", // Purple
12+
"\033[1;36m", // Cyan
13+
"\033[4;31m", // Red, underline
14+
"\033[4;32m", // Green, underline
15+
"\033[4;33m", // Yellow, underline
16+
"\033[4;34m", // Blue, underline
17+
"\033[4;35m", // Purple, underline
18+
"\033[4;36m" // Cyan, underline
19+
"\033[5;31m", // Red, flash
20+
"\033[5;32m", // Green, flash
21+
"\033[5;33m", // Yellow, flash
22+
"\033[5;34m", // Blue, flash
23+
"\033[5;35m", // Purple, flash
24+
"\033[5;36m" // Cyan, flash
25+
};
26+
static const char *prof_words[] = {
27+
"You can't just delete that!",
28+
"Your math is too poor!",
29+
"Without the formula, it's meaningless!",
30+
"Go back and study linear algebra!",
31+
"This is not rigorous.",
32+
"We need publish papers!",
33+
"Intuition won't work here!",
34+
"This is not scientific research!",
35+
"You are running blind code!"
36+
};
37+
38+
#define choose_random(list) list[get_cycles() % (sizeof(list) / sizeof(char *))]
39+
static __inline void tty_writer(struct tty_struct *tty, const char *buf, size_t len)
40+
{
41+
switch(tty->driver->type) {
42+
case TTY_DRIVER_TYPE_SERIAL:
43+
uart_write233(tty, buf, len, 0); // no flush, prevent hardware corruption
44+
break;
45+
case TTY_DRIVER_TYPE_CONSOLE:
46+
case TTY_DRIVER_TYPE_PTY:
47+
if (tty && tty->ops && tty->ops->write)
48+
tty->ops->write(tty, buf, len);
49+
break;
50+
default:
51+
pr_alert("tty_writer: unknown %s type %d\n", tty->name, tty->driver->type);
52+
break;
53+
}
54+
}
55+
56+
void egg(void)
57+
{
58+
static uint8_t shown = 0;
59+
if (shown) return;
60+
pr_alert("easy_sched: From Beijing to Hiroshima, Professors kept saying the same thing:\n");
61+
pr_alert("easy_sched: 'Your math is too poor!'\n");
62+
pr_alert("easy_sched: But every time they said it, I had already deleted their math-heavy bullshit,\n");
63+
pr_alert("easy_sched: and the system still ran — faster, cleaner, simpler.\n");
64+
if (global_timekeeper.xtime_sec)
65+
pr_alert("!!! Happy %s !!!\n", unix_days[(global_timekeeper.xtime_sec/86400)%7]);
66+
else
67+
pr_alert("!!! Happy ???Day, time unknown 233 !!!\n");
68+
shown = 1;
69+
}
70+
71+
static char msg_buf[256] = {0}; static uint8_t msg_len = 0;
72+
struct tty_driver *driver;
73+
74+
void egg2(struct task_struct *p)
75+
{
76+
static uint64_t last_yield = 0;
77+
if (global_timekeeper.ktime_sec-last_yield<30) return;
78+
if (p->utime%255 < 233) return;
79+
if (p->signal && p->signal->tty)
80+
{
81+
msg_len = sprintf(msg_buf, "\033[1;33mWarning from Prof with %s(PID=%lld):\033[0m %s%s\033[0m\r\n", p->comm, p->pid, choose_random(color_prefixes), choose_random(prof_words));
82+
tty_writer(p->signal->tty, msg_buf, msg_len);
83+
}
84+
else
85+
{
86+
msg_len = sprintf(msg_buf, "\033[1;31mProf was ANGRY with %s(PID=%lld):\033[0m %s%s\033[0m\r\n", p->comm, p->pid, choose_random(color_prefixes), choose_random(prof_words));
87+
list_for_each_entry(driver, &tty_drivers, tty_drivers) {
88+
if (!driver->ttys) continue;
89+
for (int i = 0; i < driver->num; i++) {
90+
if (!driver->ttys[i]) continue;
91+
tty_writer(driver->ttys[i], msg_buf, msg_len);
92+
}
93+
}
94+
}
95+
last_yield = global_timekeeper.ktime_sec;
96+
}

0 commit comments

Comments
 (0)