Skip to content

Commit

Permalink
update readme; add copyright
Browse files Browse the repository at this point in the history
  • Loading branch information
yangminz committed Jun 22, 2021
1 parent 34fecd1 commit fcd1f68
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 141 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ Hello 观众朋友们大家好,这个Repo是我在做《深入理解计算机

**2021-05-24** 线程级并行计算时,多处理器之间维护cache的一致性(coherence)。经典的MESI协议的实现:M - Exclusively Modified; E - Exclusively Clean; S - Shared Clean; I - Invalid. [video](https://www.bilibili.com/video/BV1zq4y1E7FE/), [git-commit](https://github.com/yangminz/bcst_csapp/commit/5a30fed6968dbf8570d89069795f8787294f06ae)

**2021-06-06** 完善MESI协议的实现。拥有了多处理器之间的cache一致性,我们可以开始考虑多处理器多线程的资源共享问题。资源共享是各种问题的根源,但即便我们不考虑资源是否被正确地共享,由于MESI等cache协议,不恰当的共享会立即给性能带来极大的消耗。我们做一个简单的实验,多线程并行计算时,对同一个、相邻的、相距很远的物理地址进行读写,会得到不同的运算时间。这就是经典的伪共享(False Sharing)问题。 [video](https://www.bilibili.com/video/BV1xg411G7h2/), [git-commit](https://github.com/yangminz/bcst_csapp/commit/3e16dd4725098d1bfda3320d325dbf7c319741c3)
**2021-06-06** 完善MESI协议的实现。拥有了多处理器之间的cache一致性,我们可以开始考虑多处理器多线程的资源共享问题。资源共享是各种问题的根源,但即便我们不考虑资源是否被正确地共享,由于MESI等cache协议,不恰当的共享会立即给性能带来极大的消耗。我们做一个简单的实验,多线程并行计算时,对同一个、相邻的、相距很远的物理地址进行读写,会得到不同的运算时间。这就是经典的伪共享(False Sharing)问题。 [video](https://www.bilibili.com/video/BV1xg411G7h2/), [git-commit](https://github.com/yangminz/bcst_csapp/commit/3e16dd4725098d1bfda3320d325dbf7c319741c3)

**2021-06-14** 开始考虑虚拟内存(Virtual Machine)。对于先前的`va2pa`映射,我们考虑四种维护虚拟地址到物理地址的映射,并且要求都是O(1)时间复杂度:1. 使用`%`,对物理内存范围取余数,这也是目前为止,我们代码所采用的映射方法;2. 使用Hash Map,维护一个地址到地址的映射;3. 使用段表(Segment Table);4. 使用页表(Page Table)。使用页表时,页表本身的存放也要占据物理内存,因此我们需要想办法减少页表本身占据的空间,否则我们要分配2^36条页表项,维护每一个虚拟页(Virtual Page)与物理页(Physical Page)之间的映射。这个方法就是多级页表。 [video](https://www.bilibili.com/video/BV1hq4y1L7bs/), [git-commit](https://github.com/yangminz/bcst_csapp/commit/f9323bdb94b18879a551df2ca8c5cfa1e75bf65b)

**2021-06-20** 使用多级页表实现`va2pa`的映射。为此,我们首先需要确定每一个页表项(Page Table Entry)都储存怎样的信息。每一个VP与PP都占据4KB的大小,而一整张页表也同样这么大。4KB的一张页表维护512个次级页表的物理页号(Physical Page Number),每一项大小为8 Bytes。这8 Bytes,64 Bits中,几乎每一个Bit都有自己的含义,用于标识与控制。至此,我们大概了解了页表的数据结构,其实本质上是一个512叉树。对于这512叉树,它的根节点PPN通过控制寄存器`CR3`索引确定。 [video](https://www.bilibili.com/video/BV1hq4y1L7bs/), [git-commit](https://github.com/yangminz/bcst_csapp/commit/6c640bcb5975100eba147276df09e0fc10a1ffb0)
10 changes: 10 additions & 0 deletions src/algorithm/bst.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/* BCST - Introduction to Computer Systems
* Author: [email protected]
* Github: https://github.com/yangminz/bcst_csapp
* Bilibili: https://space.bilibili.com/4564101
* Zhihu: https://www.zhihu.com/people/zhao-yang-min
* This project (code repository and videos) is exclusively owned by yangminz
* and shall not be used for commercial and profitting purpose
* without yangminz's permission.
*/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
10 changes: 10 additions & 0 deletions src/algorithm/redblack.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/* BCST - Introduction to Computer Systems
* Author: [email protected]
* Github: https://github.com/yangminz/bcst_csapp
* Bilibili: https://space.bilibili.com/4564101
* Zhihu: https://www.zhihu.com/people/zhao-yang-min
* This project (code repository and videos) is exclusively owned by yangminz
* and shall not be used for commercial and profitting purpose
* without yangminz's permission.
*/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
280 changes: 140 additions & 140 deletions src/headers/algorithm.h
Original file line number Diff line number Diff line change
@@ -1,140 +1,140 @@
/* BCST - Introduction to Computer Systems
* Author: [email protected]
* Github: https://github.com/yangminz/bcst_csapp
* Bilibili: https://space.bilibili.com/4564101
* Zhihu: https://www.zhihu.com/people/zhao-yang-min
* This project (code repository and videos) is exclusively owned by yangminz
* and shall not be used for commercial and profitting purpose
* without yangminz's permission.
*/

// include guards to prevent double declaration of any identifiers
// such as types, enums and static variables
#ifndef DATASTRUCTURE_GUARD
#define DATASTRUCTURE_GUARD

#include <stdint.h>

/*======================================*/
/* Circular Doubly Linked List */
/*======================================*/
typedef struct LINKED_LIST_NODE_STRUCT
{
uint64_t value;
struct LINKED_LIST_NODE_STRUCT *prev;
struct LINKED_LIST_NODE_STRUCT *next;
} linkedlist_node_t;

typedef struct
{
linkedlist_node_t *head;
uint64_t count;
} linkedlist_t;

linkedlist_t *linkedlist_construct();
void linkedlist_free(linkedlist_t *list);
linkedlist_t *linkedlist_add(linkedlist_t *list, uint64_t value);
int linkedlist_delete(linkedlist_t *list, linkedlist_node_t *node);
linkedlist_node_t *linkedlist_get(linkedlist_t *list, uint64_t value);
linkedlist_node_t *linkedlist_next(linkedlist_t *list);

/*======================================*/
/* Dynamic Array */
/*======================================*/
typedef struct
{
uint32_t size;
uint32_t count;
uint64_t *table;
} array_t;

array_t *array_construct(int size);
void array_free(array_t *arr);
array_t *array_insert(array_t *arr, uint64_t value);
int array_delete(array_t *arr, int index);
int array_get(array_t *arr, int index, uint64_t *valptr);

/*======================================*/
/* Extendible Hash Table */
/*======================================*/
typedef struct
{
int localdepth; // the local depth
int counter; // the counter of slots (have data)
char **karray;
uint64_t *varray;
} hashtable_bucket_t;

typedef struct
{
int num; // number of buckets = 1 << globaldepth
int globaldepth; // the global depth

int size; // the size of (key, value) tuples of each bucket
hashtable_bucket_t **directory; // the internal table is actually an array
} hashtable_t;

hashtable_t *hashtable_construct(int size);
void hashtable_free(hashtable_t *tab);
int hashtable_get(hashtable_t *tab, char *key, uint64_t *valptr);
hashtable_t *hashtable_insert(hashtable_t *tab, char *key, uint64_t val);

/*======================================*/
/* Trie - Prefix Tree */
/*======================================*/
typedef struct TRIE_NODE_STRUCT
{
hashtable_t *next;
uint64_t value;
int isvalue;
} trie_node_t;

trie_node_t * trie_construct();
void trie_free(trie_node_t *root);
trie_node_t *trie_insert(trie_node_t *root, char *key, uint64_t value);
int trie_get(trie_node_t *root, char *key, uint64_t *valptr);

/*======================================*/
/* Red Black Tree */
/*======================================*/
typedef enum
{
COLOR_RED,
COLOR_BLACK,
} rb_color_t;

typedef struct RB_NODE_STRUCT
{
// pointers
struct RB_NODE_STRUCT *parent;

union
{
struct RB_NODE_STRUCT *childs[2];
struct
{
struct RB_NODE_STRUCT *left; // childs[0]
struct RB_NODE_STRUCT *right; // childs[1]
};
};

// edge color to parent
rb_color_t color;

// tree node value
uint64_t value;
} rb_node_t;

rb_node_t *rb_insert(rb_node_t *root, uint64_t val);
rb_node_t *rb_delete(rb_node_t *root, uint64_t val);
rb_node_t *rb_find(rb_node_t *root, uint64_t val);

/*======================================*/
/* Binary Search Tree */
/*======================================*/

rb_node_t *bst_insert(rb_node_t *root, uint64_t val);
rb_node_t *bst_delete(rb_node_t *root, uint64_t val);
rb_node_t *bst_find(rb_node_t *root, uint64_t val);

#endif
/* BCST - Introduction to Computer Systems
* Author: [email protected]
* Github: https://github.com/yangminz/bcst_csapp
* Bilibili: https://space.bilibili.com/4564101
* Zhihu: https://www.zhihu.com/people/zhao-yang-min
* This project (code repository and videos) is exclusively owned by yangminz
* and shall not be used for commercial and profitting purpose
* without yangminz's permission.
*/

// include guards to prevent double declaration of any identifiers
// such as types, enums and static variables
#ifndef DATASTRUCTURE_GUARD
#define DATASTRUCTURE_GUARD

#include <stdint.h>

/*======================================*/
/* Circular Doubly Linked List */
/*======================================*/
typedef struct LINKED_LIST_NODE_STRUCT
{
uint64_t value;
struct LINKED_LIST_NODE_STRUCT *prev;
struct LINKED_LIST_NODE_STRUCT *next;
} linkedlist_node_t;

typedef struct
{
linkedlist_node_t *head;
uint64_t count;
} linkedlist_t;

linkedlist_t *linkedlist_construct();
void linkedlist_free(linkedlist_t *list);
linkedlist_t *linkedlist_add(linkedlist_t *list, uint64_t value);
int linkedlist_delete(linkedlist_t *list, linkedlist_node_t *node);
linkedlist_node_t *linkedlist_get(linkedlist_t *list, uint64_t value);
linkedlist_node_t *linkedlist_next(linkedlist_t *list);

/*======================================*/
/* Dynamic Array */
/*======================================*/
typedef struct
{
uint32_t size;
uint32_t count;
uint64_t *table;
} array_t;

array_t *array_construct(int size);
void array_free(array_t *arr);
array_t *array_insert(array_t *arr, uint64_t value);
int array_delete(array_t *arr, int index);
int array_get(array_t *arr, int index, uint64_t *valptr);

/*======================================*/
/* Extendible Hash Table */
/*======================================*/
typedef struct
{
int localdepth; // the local depth
int counter; // the counter of slots (have data)
char **karray;
uint64_t *varray;
} hashtable_bucket_t;

typedef struct
{
int num; // number of buckets = 1 << globaldepth
int globaldepth; // the global depth

int size; // the size of (key, value) tuples of each bucket
hashtable_bucket_t **directory; // the internal table is actually an array
} hashtable_t;

hashtable_t *hashtable_construct(int size);
void hashtable_free(hashtable_t *tab);
int hashtable_get(hashtable_t *tab, char *key, uint64_t *valptr);
hashtable_t *hashtable_insert(hashtable_t *tab, char *key, uint64_t val);

/*======================================*/
/* Trie - Prefix Tree */
/*======================================*/
typedef struct TRIE_NODE_STRUCT
{
hashtable_t *next;
uint64_t value;
int isvalue;
} trie_node_t;

trie_node_t * trie_construct();
void trie_free(trie_node_t *root);
trie_node_t *trie_insert(trie_node_t *root, char *key, uint64_t value);
int trie_get(trie_node_t *root, char *key, uint64_t *valptr);

/*======================================*/
/* Red Black Tree */
/*======================================*/
typedef enum
{
COLOR_RED,
COLOR_BLACK,
} rb_color_t;

typedef struct RB_NODE_STRUCT
{
// pointers
struct RB_NODE_STRUCT *parent;

union
{
struct RB_NODE_STRUCT *childs[2];
struct
{
struct RB_NODE_STRUCT *left; // childs[0]
struct RB_NODE_STRUCT *right; // childs[1]
};
};

// edge color to parent
rb_color_t color;

// tree node value
uint64_t value;
} rb_node_t;

rb_node_t *rb_insert(rb_node_t *root, uint64_t val);
rb_node_t *rb_delete(rb_node_t *root, uint64_t val);
rb_node_t *rb_find(rb_node_t *root, uint64_t val);

/*======================================*/
/* Binary Search Tree */
/*======================================*/

rb_node_t *bst_insert(rb_node_t *root, uint64_t val);
rb_node_t *bst_delete(rb_node_t *root, uint64_t val);
rb_node_t *bst_find(rb_node_t *root, uint64_t val);

#endif

0 comments on commit fcd1f68

Please sign in to comment.