Skip to content

Commit 303e230

Browse files
committed
Complete docs
1 parent 74bc0cb commit 303e230

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1279
-868
lines changed

analyzer.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,18 @@ void *analyzer(void *arg) {
3030
unsigned long *avg;
3131
unsigned long *bufforAvg;
3232
struct proc_stat *stats;
33-
33+
3434
(void)arg;
35-
36-
if ((prev = malloc((unsigned long)g_nproc * sizeof(struct proc_stat))) == NULL) {
35+
36+
if ((prev = malloc((unsigned long)g_nproc * sizeof(struct proc_stat))) ==
37+
NULL) {
3738
return NULL;
3839
}
3940
if ((avg = malloc((unsigned long)g_nproc * sizeof(unsigned long))) == NULL) {
4041
free(prev);
4142
return NULL;
4243
}
43-
pthread_cleanup_push(free, prev)
44-
pthread_cleanup_push(free, avg)
45-
while (1) {
44+
pthread_cleanup_push(free, prev) pthread_cleanup_push(free, avg) while (1) {
4645
notify_watchdog(Analyzer);
4746

4847
sem_wait(&g_dataFilledSpaceSemaphore);

cputracker.c

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "printer.h"
55
#include "reader.h"
66

7-
87
int g_nproc = 0;
98
static struct proc_stat *g_dataBuffer[BUFFER_SIZE];
109
pthread_mutex_t g_dataBufferMutex = PTHREAD_MUTEX_INITIALIZER;
@@ -20,17 +19,27 @@ static pthread_t readerThreadId;
2019
static pthread_t analyzerThreadId;
2120
static pthread_t printerThreadId;
2221

23-
2422
static int working[THREADS];
2523
static pthread_mutex_t watchdogMutex = PTHREAD_MUTEX_INITIALIZER;
2624

25+
/**
26+
* @brief Signal handler
27+
* @param signum signal number
28+
*/
2729
static void handler(int signum) {
2830
(void)signum;
2931
pthread_cancel(readerThreadId);
3032
pthread_cancel(printerThreadId);
3133
pthread_cancel(analyzerThreadId);
3234
}
3335

36+
/**
37+
* @brief Function retrieving the number of threads
38+
*
39+
* @param signum signal number
40+
*
41+
* @return 0 if the number of threads has been retrieved else -1
42+
*/
3443
static int get_nproc(int *nproc) {
3544
*nproc = (int)sysconf(_SC_NPROCESSORS_ONLN);
3645
if (*nproc == -1) {
@@ -39,28 +48,56 @@ static int get_nproc(int *nproc) {
3948
return 0;
4049
}
4150

51+
/**
52+
* @brief Function getting the semaphore value
53+
*
54+
* @param sem semaphore
55+
*
56+
* @return sem value
57+
*/
4258
static int get_semaphore_value(sem_t *sem) {
4359
int sval;
4460
sem_getvalue(sem, &sval);
4561
return sval;
4662
}
4763

64+
/**
65+
* @brief Function getting item in data buffer
66+
*
67+
* @return proc_stat struct from data buffer
68+
*/
4869
struct proc_stat *get_item_from_data_buffer(void) {
4970
int index = get_semaphore_value(&g_dataFilledSpaceSemaphore);
5071
return g_dataBuffer[index];
5172
}
5273

74+
/**
75+
* @brief Function getting item in print buffer
76+
*
77+
* @return proc_stat struct from print buffer
78+
*/
5379
unsigned long *get_item_from_print_buffer(void) {
5480
int index = get_semaphore_value(&g_printFilledSpaceSemaphore);
5581
return g_printBuffer[index];
5682
}
5783

58-
void notify_watchdog(int id){
84+
/**
85+
* @brief Notify the watchdog that thread is alive
86+
*
87+
* @param id Thread ID
88+
*/
89+
void notify_watchdog(int id) {
5990
pthread_mutex_lock(&watchdogMutex);
6091
working[id] = 1;
6192
pthread_mutex_unlock(&watchdogMutex);
6293
}
6394

95+
/**
96+
* @brief Watchdog function.
97+
*
98+
* This function checks whether the threads are alive.
99+
* If a thread does not speak for 2 seconds the watchdog kills the program
100+
*/
64101
static void watchdog(void) {
65102
while (1) {
66103
sleep(TIMEOUT);
@@ -78,7 +115,10 @@ static void watchdog(void) {
78115
}
79116
}
80117

81-
void main_test(void){
118+
/**
119+
* @brief Simple test function with asserts.
120+
*/
121+
void main_test(void) {
82122
int nproc;
83123
if (get_nproc(&nproc) == 0) {
84124
assert(nproc >= 1);

cputracker.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* @brief Enum with Threads id
2424
*/
25-
enum Ids {Reader = 0, Printer = 1, Analyzer = 2};
25+
enum Ids { Reader = 0, Printer = 1, Analyzer = 2 };
2626

2727
/**
2828
* @brief Struct for /proc/stats info.
@@ -33,19 +33,18 @@ struct proc_stat {
3333
nice, /**< Time spent with niced processes in user mode.*/
3434
system, /**< Time spent running in kernel mode.*/
3535
idle, /**< Time spent in vacations twiddling thumbs.*/
36-
iowait, /**< Time spent waiting for I/O to completed. This is considered
37-
idle time too.*/
38-
irq, /**< Time spent serving hardware interrupts.*/
39-
softirq, /**< Time spent serving software interrupts.*/
40-
steal, /**< Time stolen by other operating systems running in a virtual
41-
environment.*/
42-
guest, /**< Time spent for running a virtual CPU or guest OS under the
43-
control of the kernel.*/
36+
iowait, /**< Time spent waiting for I/O to completed. This is considered
37+
idle time too.*/
38+
irq, /**< Time spent serving hardware interrupts.*/
39+
softirq, /**< Time spent serving software interrupts.*/
40+
steal, /**< Time stolen by other operating systems running in a virtual
41+
environment.*/
42+
guest, /**< Time spent for running a virtual CPU or guest OS under the
43+
control of the kernel.*/
4444
guest_nice; /**< Time spent running a niced guest (virtual CPU for guest
4545
operating systems under the control of the Linux kernel).*/
4646
};
4747

48-
4948
extern int g_nproc;
5049
extern pthread_mutex_t g_dataBufferMutex;
5150
extern sem_t g_dataFilledSpaceSemaphore;

docs/html/analyzer_8c.html

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -106,48 +106,50 @@ <h2 class="memtitle"><span class="permalink"><a href="#a60d1e2bdc5c39558234c421a
106106
<div class="line"><span class="lineno"> 30</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *avg;</div>
107107
<div class="line"><span class="lineno"> 31</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *bufforAvg;</div>
108108
<div class="line"><span class="lineno"> 32</span> <span class="keyword">struct </span><a class="code hl_struct" href="structproc__stat.html">proc_stat</a> *stats;</div>
109-
<div class="line"><span class="lineno"> 33</span> </div>
109+
<div class="line"><span class="lineno"> 33</span> </div>
110110
<div class="line"><span class="lineno"> 34</span> (void)arg;</div>
111-
<div class="line"><span class="lineno"> 35</span> </div>
112-
<div class="line"><span class="lineno"> 36</span> <span class="keywordflow">if</span> ((prev = malloc((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)g_nproc * <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code hl_struct" href="structproc__stat.html">proc_stat</a>))) == NULL) {</div>
113-
<div class="line"><span class="lineno"> 37</span> <span class="keywordflow">return</span> NULL;</div>
114-
<div class="line"><span class="lineno"> 38</span> }</div>
115-
<div class="line"><span class="lineno"> 39</span> <span class="keywordflow">if</span> ((avg = malloc((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)g_nproc * <span class="keyword">sizeof</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>))) == NULL) {</div>
116-
<div class="line"><span class="lineno"> 40</span> free(prev);</div>
117-
<div class="line"><span class="lineno"> 41</span> <span class="keywordflow">return</span> NULL;</div>
118-
<div class="line"><span class="lineno"> 42</span> }</div>
119-
<div class="line"><span class="lineno"> 43</span> pthread_cleanup_push(free, prev)</div>
120-
<div class="line"><span class="lineno"> 44</span> pthread_cleanup_push(free, avg)</div>
121-
<div class="line"><span class="lineno"> 45</span> <span class="keywordflow">while</span> (1) {</div>
122-
<div class="line"><span class="lineno"> 46</span> notify_watchdog(Analyzer);</div>
123-
<div class="line"><span class="lineno"> 47</span> </div>
124-
<div class="line"><span class="lineno"> 48</span> sem_wait(&amp;g_dataFilledSpaceSemaphore);</div>
125-
<div class="line"><span class="lineno"> 49</span> pthread_mutex_lock(&amp;g_dataBufferMutex);</div>
126-
<div class="line"><span class="lineno"> 50</span> </div>
127-
<div class="line"><span class="lineno"> 51</span> stats = get_item_from_data_buffer();</div>
128-
<div class="line"><span class="lineno"> 52</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; g_nproc; i++) {</div>
129-
<div class="line"><span class="lineno"> 53</span> avg[i] = average_cpu_usage(prev[i], stats[i]);</div>
130-
<div class="line"><span class="lineno"> 54</span> prev[i] = stats[i];</div>
131-
<div class="line"><span class="lineno"> 55</span> }</div>
132-
<div class="line"><span class="lineno"> 56</span> </div>
133-
<div class="line"><span class="lineno"> 57</span> pthread_mutex_unlock(&amp;g_dataBufferMutex);</div>
134-
<div class="line"><span class="lineno"> 58</span> sem_post(&amp;g_dataLeftSpaceSemaphore);</div>
135-
<div class="line"><span class="lineno"> 59</span> </div>
136-
<div class="line"><span class="lineno"> 60</span> <span class="comment">//</span></div>
137-
<div class="line"><span class="lineno"> 61</span> </div>
138-
<div class="line"><span class="lineno"> 62</span> sem_wait(&amp;g_printLeftSpaceSemaphore);</div>
139-
<div class="line"><span class="lineno"> 63</span> pthread_mutex_lock(&amp;g_printBufferMutex);</div>
140-
<div class="line"><span class="lineno"> 64</span> </div>
141-
<div class="line"><span class="lineno"> 65</span> bufforAvg = get_item_from_print_buffer();</div>
142-
<div class="line"><span class="lineno"> 66</span> memcpy(bufforAvg, avg, (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)g_nproc * <span class="keyword">sizeof</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>));</div>
143-
<div class="line"><span class="lineno"> 67</span> </div>
144-
<div class="line"><span class="lineno"> 68</span> pthread_mutex_unlock(&amp;g_printBufferMutex);</div>
145-
<div class="line"><span class="lineno"> 69</span> sem_post(&amp;g_printFilledSpaceSemaphore);</div>
146-
<div class="line"><span class="lineno"> 70</span> }</div>
111+
<div class="line"><span class="lineno"> 35</span> </div>
112+
<div class="line"><span class="lineno"> 36</span> <span class="keywordflow">if</span> ((prev = malloc((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)g_nproc * <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code hl_struct" href="structproc__stat.html">proc_stat</a>))) ==</div>
113+
<div class="line"><span class="lineno"> 37</span> NULL) {</div>
114+
<div class="line"><span class="lineno"> 38</span> <span class="keywordflow">return</span> NULL;</div>
115+
<div class="line"><span class="lineno"> 39</span> }</div>
116+
<div class="line"><span class="lineno"> 40</span> <span class="keywordflow">if</span> ((avg = malloc((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)g_nproc * <span class="keyword">sizeof</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>))) == NULL) {</div>
117+
<div class="line"><span class="lineno"> 41</span> free(prev);</div>
118+
<div class="line"><span class="lineno"> 42</span> <span class="keywordflow">return</span> NULL;</div>
119+
<div class="line"><span class="lineno"> 43</span> }</div>
120+
<div class="line"><span class="lineno"> 44</span> pthread_cleanup_push(free, prev) pthread_cleanup_push(free, avg) <span class="keywordflow">while</span> (1) {</div>
121+
<div class="line"><span class="lineno"> 45</span> <a class="code hl_function" href="cputracker_8c.html#ac99b6069bf629214ce6258902739d613">notify_watchdog</a>(Analyzer);</div>
122+
<div class="line"><span class="lineno"> 46</span> </div>
123+
<div class="line"><span class="lineno"> 47</span> sem_wait(&amp;g_dataFilledSpaceSemaphore);</div>
124+
<div class="line"><span class="lineno"> 48</span> pthread_mutex_lock(&amp;g_dataBufferMutex);</div>
125+
<div class="line"><span class="lineno"> 49</span> </div>
126+
<div class="line"><span class="lineno"> 50</span> stats = <a class="code hl_function" href="cputracker_8c.html#a6d7b4993bb9144e4cab9f9c7420c996a">get_item_from_data_buffer</a>();</div>
127+
<div class="line"><span class="lineno"> 51</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; g_nproc; i++) {</div>
128+
<div class="line"><span class="lineno"> 52</span> avg[i] = average_cpu_usage(prev[i], stats[i]);</div>
129+
<div class="line"><span class="lineno"> 53</span> prev[i] = stats[i];</div>
130+
<div class="line"><span class="lineno"> 54</span> }</div>
131+
<div class="line"><span class="lineno"> 55</span> </div>
132+
<div class="line"><span class="lineno"> 56</span> pthread_mutex_unlock(&amp;g_dataBufferMutex);</div>
133+
<div class="line"><span class="lineno"> 57</span> sem_post(&amp;g_dataLeftSpaceSemaphore);</div>
134+
<div class="line"><span class="lineno"> 58</span> </div>
135+
<div class="line"><span class="lineno"> 59</span> <span class="comment">//</span></div>
136+
<div class="line"><span class="lineno"> 60</span> </div>
137+
<div class="line"><span class="lineno"> 61</span> sem_wait(&amp;g_printLeftSpaceSemaphore);</div>
138+
<div class="line"><span class="lineno"> 62</span> pthread_mutex_lock(&amp;g_printBufferMutex);</div>
139+
<div class="line"><span class="lineno"> 63</span> </div>
140+
<div class="line"><span class="lineno"> 64</span> bufforAvg = <a class="code hl_function" href="cputracker_8c.html#a2b8f2341417ff6536dcfd85113bd3a5d">get_item_from_print_buffer</a>();</div>
141+
<div class="line"><span class="lineno"> 65</span> memcpy(bufforAvg, avg, (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)g_nproc * <span class="keyword">sizeof</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>));</div>
142+
<div class="line"><span class="lineno"> 66</span> </div>
143+
<div class="line"><span class="lineno"> 67</span> pthread_mutex_unlock(&amp;g_printBufferMutex);</div>
144+
<div class="line"><span class="lineno"> 68</span> sem_post(&amp;g_printFilledSpaceSemaphore);</div>
145+
<div class="line"><span class="lineno"> 69</span> }</div>
146+
<div class="line"><span class="lineno"> 70</span> pthread_cleanup_pop(1);</div>
147147
<div class="line"><span class="lineno"> 71</span> pthread_cleanup_pop(1);</div>
148-
<div class="line"><span class="lineno"> 72</span> pthread_cleanup_pop(1);</div>
149-
<div class="line"><span class="lineno"> 73</span>}</div>
150-
<div class="ttc" id="astructproc__stat_html"><div class="ttname"><a href="structproc__stat.html">proc_stat</a></div><div class="ttdoc">Use brief, otherwise the index won't have a brief explanation.</div><div class="ttdef"><b>Definition:</b> <a href="cputracker_8h_source.html#l00028">cputracker.h:28</a></div></div>
148+
<div class="line"><span class="lineno"> 72</span>}</div>
149+
<div class="ttc" id="acputracker_8c_html_a2b8f2341417ff6536dcfd85113bd3a5d"><div class="ttname"><a href="cputracker_8c.html#a2b8f2341417ff6536dcfd85113bd3a5d">get_item_from_print_buffer</a></div><div class="ttdeci">unsigned long * get_item_from_print_buffer(void)</div><div class="ttdoc">Function getting item in print buffer.</div><div class="ttdef"><b>Definition:</b> <a href="cputracker_8c_source.html#l00079">cputracker.c:79</a></div></div>
150+
<div class="ttc" id="acputracker_8c_html_a6d7b4993bb9144e4cab9f9c7420c996a"><div class="ttname"><a href="cputracker_8c.html#a6d7b4993bb9144e4cab9f9c7420c996a">get_item_from_data_buffer</a></div><div class="ttdeci">struct proc_stat * get_item_from_data_buffer(void)</div><div class="ttdoc">Function getting item in data buffer.</div><div class="ttdef"><b>Definition:</b> <a href="cputracker_8c_source.html#l00069">cputracker.c:69</a></div></div>
151+
<div class="ttc" id="acputracker_8c_html_ac99b6069bf629214ce6258902739d613"><div class="ttname"><a href="cputracker_8c.html#ac99b6069bf629214ce6258902739d613">notify_watchdog</a></div><div class="ttdeci">void notify_watchdog(int id)</div><div class="ttdoc">Notify the watchdog that thread is alive.</div><div class="ttdef"><b>Definition:</b> <a href="cputracker_8c_source.html#l00089">cputracker.c:89</a></div></div>
152+
<div class="ttc" id="astructproc__stat_html"><div class="ttname"><a href="structproc__stat.html">proc_stat</a></div><div class="ttdoc">Struct for /proc/stats info.</div><div class="ttdef"><b>Definition:</b> <a href="cputracker_8h_source.html#l00030">cputracker.h:30</a></div></div>
151153
</div><!-- fragment -->
152154
</div>
153155
</div>

0 commit comments

Comments
 (0)