Skip to content

Commit

Permalink
pass proj1
Browse files Browse the repository at this point in the history
  • Loading branch information
AstatineAi committed Mar 6, 2024
1 parent bce7efc commit 151cfd6
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 20 deletions.
35 changes: 32 additions & 3 deletions lecture_note/docs/pintos/proj1.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ TA 拿锁 B, TA 捐赠给 B, B 捐赠给 TB, interrupt
TB 拿锁 A, TB 捐赠给 A, A 捐赠给 TA, TA 捐赠给 B...
```
解决?方式: 对于嵌套优先级捐赠关系的环 (From document "If necessary, you may impose a reasonable limit on depth of nested priority donation, such as 8 levels."), 加一个传递层数即可.
解决?方式: 对于嵌套优先级捐赠关系的环 (From document "If necessary, you may impose a reasonable limit on depth of nested priority donation, such as 8 levels."), 加一个传递层数?.
外界死锁实在没法解决, 但是属于 UB, 不解决也行. 可以考虑加一个检查是否所有的线程都被 block, 如果如此就立一个 flag 清除所有的线程, 然后 PANIC.
Expand All @@ -262,7 +262,7 @@ TB 拿锁 A, TB 捐赠给 A, A 捐赠给 TA, TA 捐赠给 B...
问题: 被更新 priority 的 thread 还在堆 / ready_list 里面, 但是插入已经发生了, 该 heap_up / heap_down?
解决?方式: 不管, 下一次进堆的时候就好了.
解决?方式: 在完成从叶子节点到根节点的 donation 之后, 禁用中断然后 $O(n)$ 建堆.
## Task 3
Expand All @@ -272,4 +272,33 @@ TB 拿锁 A, TB 捐赠给 A, A 捐赠给 TA, TA 捐赠给 B...
在 pintos 内核中没有浮点数运算, 需要手动实现定点数来完成 `load_avg`, `nice` 等 MLFQS 相关值的计算.
使用 32 位 `int` 保存一个定点数, 令 `x` 表示实数 `x / (2^14)`, 即使用最低 14 位存储分数.
使用 32 位 `int` 保存一个定点数, 令 `x` 表示实数 `x / (2^14)`, 即使用最低 14 位存储分数.
### 更改 nice
nice 只来自外界传入 / 从 parent thread 继承, 修改后更新优先级即可.
### 计算 priority
$$
priority = PRI_MAX - \dfrac{1}{4} recent_cpu - 2 \cdot nice
$$
需要控制范围不超过 `PRI_MIN` 到 `PRI_MAX`.
### 计算 recent_cpu
$$
recent_cpu = \dfrac{2 \cdot load_avg}{2 \cdot load_avg + 1} \cdor recent_cpu + nice
$$
### 计算 load_avg
`load_avg` 是全局的, 不是某个线程的属性.
$$
load_avg = \dfrac{59}{60} load_avg + \dfrac{1}{60} ready_thread
$$
$ready_thread$ 代表正在运行/就绪的线程数量.
1 change: 0 additions & 1 deletion pintos/src/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ threads_SRC += threads/intr-stubs.S # Interrupt stubs.
threads_SRC += threads/synch.c # Synchronization.
threads_SRC += threads/palloc.c # Page allocator.
threads_SRC += threads/malloc.c # Subpage allocator.
threads_SRC += threads/fixed-point.c # Fixed-Point real arithmetic.

# Device driver code.
devices_SRC = devices/pit.c # Programmable interrupt timer chip.
Expand Down
9 changes: 9 additions & 0 deletions pintos/src/lib/kernel/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ heap_init (struct heap *heap, heap_less_func *less)
heap->push_cnt = 0;
}

/* Rebuild heap */
void
heap_rebuild (struct heap *heap)
{
ASSERT (heap != NULL);
for (size_t i = heap->size; i > 0; i--)
down_heap (heap, i);
}

heap_elem
heap_top (struct heap *heap)
{
Expand Down
1 change: 1 addition & 0 deletions pintos/src/lib/kernel/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct heap

/* Heap initialization. */
void heap_init (struct heap *, heap_less_func *);
void heap_rebuild (struct heap *);

/* Heap elements. */
heap_elem heap_top (struct heap *);
Expand Down
3 changes: 0 additions & 3 deletions pintos/src/threads/fixed-point.c

This file was deleted.

73 changes: 73 additions & 0 deletions pintos/src/threads/fixed-point.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,80 @@
#define THREADS_FIXED_POINT_H

#include <debug.h>
#include <stdint.h>

typedef int fixed_point;

#define F_BITS 16384

/* Integer to fixed point number */
static inline fixed_point
itofp (const int x)
{
return x * F_BITS;
}

/* Fixed point to integer */
static inline int
fptoi (const fixed_point x)
{
return x / F_BITS;
}

/* Fixed point to integer (round) */
static inline int
fptoi_round (const fixed_point x)
{
return x >= 0 ? (x + F_BITS / 2) / F_BITS :
(x - F_BITS / 2) / F_BITS;
}

static inline fixed_point
fp_add (const fixed_point a, const fixed_point b)
{
return a + b;
}

static inline fixed_point
fp_sub (const fixed_point a, const fixed_point b)
{
return a - b;
}

static inline fixed_point
fp_mul (const fixed_point a, const fixed_point b)
{
return ((int64_t)a * b) / F_BITS;
}

static inline fixed_point
fp_div (const fixed_point a, const fixed_point b)
{
return ((int64_t)a * F_BITS) / b;
}

static inline fixed_point
fp_int_add (const fixed_point a, const int b)
{
return a + b * F_BITS;
}

static inline fixed_point
fp_int_sub (const fixed_point a, const int b)
{
return a - b * F_BITS;
}

static inline fixed_point
fp_int_mul (const fixed_point a, const int b)
{
return a * b;
}

static inline fixed_point
fp_int_div (const fixed_point a, const int b)
{
return a / b;
}

#endif /* threads/fixed-point.h */
14 changes: 12 additions & 2 deletions pintos/src/threads/synch.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ sema_up (struct semaphore *sema)
}
sema->value++;
intr_set_level (old_level);
thread_yield ();
if (intr_context ())
intr_yield_on_return ();
else
thread_yield ();
}

static void sema_test_helper (void *sema_);
Expand Down Expand Up @@ -213,6 +216,11 @@ lock_acquire (struct lock *lock)
{
cur->lock_waiting = lock;
donate_priority (cur, 0);

old_level = intr_disable ();
sort_ready_heap ();
intr_set_level (old_level);

sema_down (&lock->semaphore);

old_level = intr_disable ();
Expand Down Expand Up @@ -265,7 +273,7 @@ lock_release (struct lock *lock)
list_remove (&lock->elem);

intr_set_level (old_level);
thread_update_priority (thread_current ());
thread_update_priority (thread_current (), NULL);
sema_up (&lock->semaphore);
}

Expand All @@ -274,6 +282,8 @@ lock_release (struct lock *lock)
void
donate_priority (struct thread *t, int dep)
{
if (thread_mlfqs)
return;
ASSERT (t != NULL);

if (dep > DONATE_MAX_DEPTH || t->lock_waiting == NULL)
Expand Down
Loading

0 comments on commit 151cfd6

Please sign in to comment.