Compare commits

..

3 Commits

Author SHA1 Message Date
Wenchao Hao 8478ddacda update
Signed-off-by: Wenchao Hao <haowenchao22@gmail.com>
2024-11-19 19:57:29 +08:00
Wenchao Hao a78d53518f add
Signed-off-by: Wenchao Hao <haowenchao22@gmail.com>
2024-11-19 19:56:45 +08:00
Wenchao Hao 36db457bf7 update
Signed-off-by: Wenchao Hao <haowenchao22@gmail.com>
2024-11-19 19:47:41 +08:00
3 changed files with 175 additions and 38 deletions

6
bak.c
View File

@ -27,7 +27,6 @@ void main()
exit(-1);
}
for (i = 0; i < 100000; i++) {
madvise(p, SIZE, MADV_HUGEPAGE);
memset(p, 0x11, SIZE); /* write to get mem */
@ -38,16 +37,15 @@ void main()
printf("swp out bandwidth: %ld bytes/ms\n",
SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b)));
//pid = fork();
pid = fork();
//if (pid) {
if (pid) {
//scanf("%c", &c);
gettimeofday(&tv_b, NULL);
memset(p, 0x33, SIZE); /* write to get mem */
gettimeofday(&tv_e, NULL);
printf("swp in bandwidth: %ld bytes/ms\n",
SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b)));
//}
}
while(1) {

109
madvise.c
View File

@ -7,20 +7,43 @@
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <openssl/md5.h>
#define SIZE 19*1024*1024
#define PAGE_SIZE 4096 // 4KB
#define CHUNK_SIZE 8 * 1024 // 8KB
void compute_md5(const unsigned char *data, size_t length, unsigned char *md5_hash) {
MD5_CTX md5_ctx;
MD5_Init(&md5_ctx);
MD5_Update(&md5_ctx, data, length);
MD5_Final(md5_hash, &md5_ctx);
}
void print_md5(unsigned char *md5_hash) {
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
printf("%02x", md5_hash[i]);
}
printf("\n");
}
unsigned long long tv_to_ms(struct timeval tv)
{
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
void main()
int main()
{
char c;
pid_t pid;
int i;
int j = 0;
struct timeval tv_b, tv_e;;
#define SIZE 100*1024*1024
unsigned char md5_hash[MD5_DIGEST_LENGTH];
unsigned long *p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (!p) {
@ -28,12 +51,25 @@ void main()
exit(-1);
}
printf("0x%lx\n", p);
madvise(p, SIZE, MADV_HUGEPAGE);
for (j = 0; j < SIZE / 8; j+=128)
for (j = 0; j < SIZE / 8; j += 8)
*(p + j) = j;
for (i = 0; i < 3; i++) {
for (size_t offset = PAGE_SIZE; offset < SIZE; offset += CHUNK_SIZE) {
char *str = (char *)p + offset;
sprintf(str, "child");
}
for (size_t offset = PAGE_SIZE; offset < SIZE; offset += CHUNK_SIZE) {
compute_md5((unsigned char *)p + offset, PAGE_SIZE, md5_hash);
printf("MD5 of the buffer: ");
print_md5(md5_hash);
}
gettimeofday(&tv_b, NULL);
madvise(p, SIZE, MADV_PAGEOUT);
gettimeofday(&tv_e, NULL);
@ -41,17 +77,62 @@ void main()
printf("swp out bandwidth: %ld bytes/ms\n",
SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b)));
gettimeofday(&tv_b, NULL);
for (j = 0; j < SIZE / 8; j+=128)
*(p + j) = j;
gettimeofday(&tv_e, NULL);
printf("swp in bandwidth: %ld bytes/ms\n",
SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b)));
pid = fork();
if (pid == 0) {
printf("child: freeing some swap\n");
for (size_t offset = 0; offset < SIZE; offset += CHUNK_SIZE) {
if (madvise((void *)((char *)p + offset), PAGE_SIZE, MADV_DONTNEED) == -1) {
perror("madvise failed");
return -1;
}
}
//madvise(p, SIZE, MADV_PAGEOUT);
printf("child: sleep\n");
sleep(5);
//while(1) {
// sleep(100);
//}
printf("child: reading swaped data\n");
for (size_t offset = PAGE_SIZE; offset < SIZE; offset += CHUNK_SIZE) {
char *str = (char *)p + offset;
if (strcmp(str, "child"))
printf("%s\n", str);
}
printf("child: read done\n");
for (size_t offset = PAGE_SIZE; offset < SIZE; offset += CHUNK_SIZE) {
compute_md5((unsigned char *)p + offset, PAGE_SIZE, md5_hash);
printf("MD5 of the buffer: ");
print_md5(md5_hash);
}
#if 2
} else {
printf("parent: sleeping\n");
sleep(1);
printf("parent: unmapping origin memory region\n");
munmap(p, SIZE);
printf("parent: alloc new memory region\n");
p = mmap(NULL, 9 * 1024 * 1024, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
for (j = 0; j < 1024 * 1024; j+=8)
*(p + j) = j + 10;
for (size_t offset = 0; offset < 8 * 1024 * 1024; offset += CHUNK_SIZE) {
char *str = (char *)p + offset + PAGE_SIZE;
sprintf(str, "parent");
}
madvise(p, 9 * 1024 * 1024, MADV_PAGEOUT);
#endif
}
while(1) {
sleep(100);
}
return 0;
}

58
test_swap.c Normal file
View File

@ -0,0 +1,58 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <openssl/md5.h>
#define SIZE 500*1024*1024
#define PAGE_SIZE 4096 // 4KB
#define CHUNK_SIZE 8 * 1024 // 8KB
unsigned long long tv_to_ms(struct timeval tv)
{
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
int main()
{
int i;
char *c;
unsigned long *p;
struct timeval tv_b, tv_e;;
p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (!p) {
perror("fail to get memory");
exit(-1);
}
c = (unsigned char *)p;
/* fill data */
for (i = 0; i < SIZE / 8; i += 8)
*(p + i) = i;
/* trigger pageout and get time of pageout */
gettimeofday(&tv_b, NULL);
madvise(p, SIZE, MADV_PAGEOUT);
gettimeofday(&tv_e, NULL);
printf("swp out bandwidth: %ld bytes/ms\n",
SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b)));
/* read to trigger swapin and */
gettimeofday(&tv_b, NULL);
for (i = 0; i < SIZE; i += PAGE_SIZE)
*(c + i) = i;
gettimeofday(&tv_e, NULL);
printf("swp in bandwidth: %ld bytes/ms\n",
SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b)));
return 0;
}