-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tezc
committed
Dec 27, 2020
1 parent
aac045c
commit 8734d6d
Showing
13 changed files
with
310 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,27 @@ | ||
|
||
#include "sc_mmap.h" | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int rc; | ||
struct sc_mmap mmap; | ||
|
||
rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC, | ||
PROT_READ | PROT_WRITE, MAP_SHARED, 0, 15000); | ||
assert(rc == 0); | ||
|
||
void* ptr = mmap.ptr; | ||
size_t mapped_len = mmap.len; | ||
|
||
printf("mapped len : %zu \n", mapped_len); | ||
|
||
*(char*)ptr = 't'; | ||
|
||
sc_mmap_msync(&mmap, 0, 4096); | ||
sc_mmap_term(&mmap); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Mutex | ||
|
||
### Mutex wrapper | ||
|
||
- Basic mutex wrapper for Unixes and Windows. | ||
- Just copy <b>sc_mutex.h</b> and <b>sc_mutex.c</b> to your project. | ||
|
||
```c | ||
|
||
#include "sc_mmap.h" | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int rc; | ||
struct sc_mmap mmap; | ||
|
||
rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC, | ||
PROT_READ | PROT_WRITE, MAP_SHARED, 0, 15000); | ||
assert(rc == 0); | ||
|
||
void* ptr = mmap.ptr; | ||
size_t mapped_len = mmap.len; | ||
|
||
printf("mapped len : %zu \n", mapped_len); | ||
|
||
*(char*)ptr = 't'; | ||
|
||
sc_mmap_msync(&mmap, 0, 4096); | ||
sc_mmap_term(&mmap); | ||
|
||
return 0; | ||
} | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Option | ||
|
||
### Portable getopt alternative | ||
|
||
- Very simple alternative to getopt. | ||
- Just copy <b>sc_opt.h</b> and <b>sc_opt.c</b> to your project. | ||
|
||
```c | ||
|
||
#include "sc_option.h" | ||
|
||
static struct sc_option_item options[] = {{.letter = 'm', .name = NULL}, | ||
{.letter = 'k', .name = "key"}, | ||
{.letter = 'h', .name = "help"}}; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
char *value; | ||
|
||
struct sc_option opt = {.argv = argv, | ||
.count = sizeof(options) / sizeof(options[0]), | ||
.options = options}; | ||
|
||
for (int i = 1; i < argc; i++) { | ||
char c = sc_option_at(&opt, i, &value); | ||
switch (c) { | ||
case 'm': | ||
// If value does not exist, it will point to '\0' character. | ||
printf("Option 'm', value : %s \n", value); | ||
break; | ||
case 'k': | ||
printf("Option 'k', value : %s \n", value); | ||
break; | ||
case 'h': | ||
printf("Option 'h', value : %s \n", value); | ||
break; | ||
case '?': | ||
printf("Unknown option : %s \n", argv[i]); | ||
break; | ||
} | ||
} | ||
|
||
|
||
return 0; | ||
}``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ | |
* SOFTWARE. | ||
*/ | ||
#ifndef SC_OPT_H | ||
|
||
#define SC_OPT_H | ||
|
||
#include <stdbool.h> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Perf benchmark | ||
|
||
### Overview | ||
|
||
- Using <i>perf_event_open</i> to get hardware and software counters. | ||
- Only useful when you want to measure something inside the code really quick. | ||
Otherwise, use <i>perf</i> itself. | ||
- Linux only. | ||
- All predefined hardware and software counters are generated in the header | ||
file, you can uncomment counters as you wish. | ||
- If some counters or combination of counters doesnt work or doesn't work as | ||
expected, please check out performance counter register allocation | ||
documentation for your CPU. CPUs have limited registers for performance | ||
counters and some counters can use specific registers only. | ||
- Just copy <b>sc_perf.h</b> and <b>sc_perf.c</b> to your project. | ||
|
||
#### Usage | ||
|
||
```c | ||
|
||
#include "sc_perf.h" | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
sc_perf_start(); | ||
|
||
long_running_operation(); | ||
|
||
sc_perf_end(); | ||
|
||
return 0; | ||
} | ||
|
||
``` | ||
##### Output will be like | ||
``` | ||
| Event | Value | Measurement time | ||
--------------------------------------------------------------- | ||
| time (seconds) | 0.66 | (100,00%) | ||
| cpu-clock | 654075766.00 | (100.00%) | ||
| task-clock | 654077198.00 | (100.00%) | ||
| page-faults | 3.00 | (100.00%) | ||
| context-switches | 46.00 | (100.00%) | ||
| cpu-migrations | 0.00 | (100.00%) | ||
| page-fault-minor | 3.00 | (100.00%) | ||
| cpu-cycles | 2656529748.00 | (100.00%) | ||
| instructions | 7589235720.00 | (100.00%) | ||
| cache-misses | 28715.00 | (100.00%) | ||
| L1D-read-miss | 34124.00 | (100.00%) | ||
| L1I-read-miss | 121958.00 | (100.00%) | ||
``` | ||
##### Pause example | ||
```c | ||
#include "sc_perf.h" | ||
int main(int argc, char *argv[]) | ||
{ | ||
sc_perf_start(); | ||
long_running_operation(); | ||
//Will stop counters. | ||
sc_perf_pause(); | ||
operation_you_dont_want_to_measure(); | ||
//Start counters again. | ||
sc_perf_start(); | ||
another_long_running_operation(); | ||
sc_perf_end(); | ||
sc_perf_start(); | ||
some_other_long_running_operation(); | ||
sc_perf_end(); | ||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Generic queue | ||
|
||
#### Overview | ||
|
||
- Type generic queue which grows when you add elements. | ||
- Add/remove from head/tail is possible so it can be used as list, stack, | ||
queue, dequeue etc. | ||
- Single allocation for all the data. | ||
- Keeps separate first and last element indexes, when you remove an element, | ||
it doesn't move elements to fill the space. | ||
- Just copy <b>sc_queue.h</b> and <b>sc_queue.c</b> to your project. | ||
|
||
|
||
##### Usage | ||
|
||
|
||
```c | ||
#include "sc_queue.h" | ||
|
||
#include <stdio.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int *queue; | ||
int elem; | ||
|
||
sc_queue_create(queue, 0); | ||
|
||
sc_queue_add_last(queue, 2); | ||
sc_queue_add_last(queue, 3); | ||
sc_queue_add_last(queue, 4); | ||
sc_queue_add_first(queue, 1); | ||
|
||
sc_queue_foreach (queue, elem) { | ||
printf("elem = [%d] \n", elem); | ||
} | ||
|
||
elem = sc_queue_del_last(queue); | ||
printf("Last element was : [%d] \n", elem); | ||
|
||
elem = sc_queue_del_first(queue); | ||
printf("First element was : [%d] \n", elem); | ||
|
||
sc_queue_destroy(queue); | ||
|
||
return 0; | ||
} | ||
``` | ||
##### Note | ||
Queue pointer is not stable, it may change if we expand the memory. If you | ||
pass the queue to another function which can add items, do it by passing | ||
reference of the queue pointer. | ||
```c | ||
void some_function_to_add_elems(long **q) | ||
{ | ||
sc_queue_add_last(*q, 500); | ||
} | ||
int main(int argc, char *argv[]) | ||
{ | ||
long *q; | ||
sc_queue_create(q, 0); | ||
sc_queue_add_last(q, 300); | ||
some_function_to_add_elems(&q); | ||
sc_array_destroy(q); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.