This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
realtime:documentation:howto:applications:memory:mlockall_globals_sample [2017/06/08 20:30] jithu made the code c style |
realtime:documentation:howto:applications:memory:mlockall_globals_sample [2017/06/10 00:28] (current) jithu |
||
---|---|---|---|
Line 3: | Line 3: | ||
<code c> | <code c> | ||
- | /* This application checks whether mlockall() forces all pages of a | + | /* |
+ | * # gcc -o check_global check_global.c -Wall -O3 | ||
+ | * # ./check_global | ||
+ | * # ./check_global -nolockmem (will cause faults) | ||
+ | * This application checks whether mlockall() forces all pages of a | ||
* global array into RAM. Normally, the OS maps a 'copy on write' MMU page | * global array into RAM. Normally, the OS maps a 'copy on write' MMU page | ||
* to such arrays meaning that reading from the array returns only zeros and | * to such arrays meaning that reading from the array returns only zeros and | ||
Line 10: | Line 14: | ||
*/ | */ | ||
#include <stdio.h> | #include <stdio.h> | ||
+ | #include <stdlib.h> | ||
#include <string.h> | #include <string.h> | ||
- | #include <unistd.h> | ||
#include <sys/mman.h> | #include <sys/mman.h> | ||
#include <sys/resource.h> | #include <sys/resource.h> | ||
+ | #include <unistd.h> | ||
/* Define the global array*/ | /* Define the global array*/ | ||
Line 23: | Line 28: | ||
static void lock_application(void) | static void lock_application(void) | ||
{ | { | ||
- | if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) | + | if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) { |
- | perror("mlockall() failed"); | + | perror("mlockall() failed - root ?"); |
+ | exit(-1); | ||
+ | } | ||
} | } | ||
Line 30: | Line 37: | ||
static int dump_page_faults(void) | static int dump_page_faults(void) | ||
{ | { | ||
+ | int new_minor_page_faults, new_major_page_faults; | ||
int page_faults_detected = 0; | int page_faults_detected = 0; | ||
- | static int init = 0; | ||
static struct rusage rusage_prev; | static struct rusage rusage_prev; | ||
+ | static int init; | ||
struct rusage rusage; | struct rusage rusage; | ||
- | int new_minor_page_faults, new_major_page_faults; | ||
getrusage(RUSAGE_SELF, &rusage); | getrusage(RUSAGE_SELF, &rusage); | ||
Line 57: | Line 64: | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
+ | int unexpected_pagefaults_detected; | ||
int lock_memory = 1; | int lock_memory = 1; | ||
+ | int buff_value; | ||
int i; | int i; | ||
- | int buff_value; | ||
- | int unexpected_pagefaults_detected; | ||
- | int l_PageSize; | ||
for (i = 1; i < argc; i++) { | for (i = 1; i < argc; i++) { | ||
Line 75: | Line 81: | ||
} | } | ||
(void)dump_page_faults(); /* Set the baseline*/ | (void)dump_page_faults(); /* Set the baseline*/ | ||
- | |||
- | /* printf something so we avoid introducing a page fault simply | ||
- | * by performing the potentially first printf call. | ||
- | */ | ||
- | l_PageSize = sysconf(_SC_PAGESIZE); | ||
- | |||
- | printf("Page size = %d\n", l_PageSize); | ||
- | |||
- | (void)dump_page_faults(); | ||
/* From this point onwards we no longer expect any page faults */ | /* From this point onwards we no longer expect any page faults */ | ||
Line 89: | Line 86: | ||
for (i = 0; i < NUM_PAGES; i++) { | for (i = 0; i < NUM_PAGES; i++) { | ||
- | int buff_value = (int) (glob_buffer[i * PAGE_SZ]); | + | buff_value = (int) (glob_buffer[i * PAGE_SZ]); |
if (buff_value != 0) { | if (buff_value != 0) { | ||
Line 98: | Line 95: | ||
unexpected_pagefaults_detected = 1; | unexpected_pagefaults_detected = 1; | ||
- | glob_buffer[(i*PAGE_SZ) + 1] = 1; | + | glob_buffer[(i * PAGE_SZ) + 1] = 1; |
if (dump_page_faults()) { | if (dump_page_faults()) { | ||
printf("Writing to page %d of global buffer caused page fault(s)\n", | printf("Writing to page %d of global buffer caused page fault(s)\n", | ||
Line 107: | Line 104: | ||
printf("Done, result: %s\n", | printf("Done, result: %s\n", | ||
- | unexpected_pagefaults_detected ? "failed":"success"); | + | unexpected_pagefaults_detected ? "failed" : "success"); |
- | return unexpected_pagefaults_detected ? 1:0; | + | return unexpected_pagefaults_detected ? 1 : 0; |
} | } | ||
</code> | </code> |