Re: Obvious one-liner - Use 3DNOW on MK8

From: Denis Vlasenko
Date: Mon Aug 23 2004 - 19:03:48 EST


These are test programs.

copy_load.c:

for(i = 0; i < N; i++)
mem[i*SIZE+1] = 'b'; /* force copy */
strchr(mem, 'c') == mem+N*SIZE-1 || printf("BUG\n"); /* read all */

This forces page copying, and then touches every byte.
--
vda
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>

#define N (256/4)
#define SIZE 4096

int main()
{
int i,k;
unsigned char *mem = mmap(0, N*SIZE, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
if(mem == MAP_FAILED)
perror("mmap");
memset(mem, 'a', N*SIZE); /* force page clearing */
mem[N*SIZE-1]='c';
//for(i = 0; i < N; i++) mem[i*SIZE] = i; /* force page clearing */

for(k = 0; k < 5000; k++) {
int pid;
pid = fork();
if(pid == 0) {
/* child */
for(i = 0; i < N; i++)
mem[i*SIZE+1] = 'b'; /* force copy */
//printf("copy complete\n");
strchr(mem, 'c') == mem+N*SIZE-1 || printf("BUG\n"); /* read all */
exit(0);
} else if(pid == -1) {
perror("fork");
} else {
/* parent */
waitpid(pid, NULL, 0);
}
}
munmap(mem, N*SIZE);
}
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>

#define N (320/4)
#define SIZE 4096

int dummy;

int main()
{
int k;
for(k = 0; k < 20000; k++) {
int i = 0;
int pid;
unsigned char *mem,*p;
mem = mmap(0, N*SIZE, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
if(mem == MAP_FAILED)
perror("mmap");
for(i = 0; i < N; i++) mem[i*SIZE] = i; /* force page allocation and clearing */
//memset(mem, 'a', N*SIZE); /* force page clearing */
p = mem;
while(p != mem+N*SIZE) { i += *p; p+=32; } /* use data */
dummy = i;
munmap(mem, N*SIZE);
}
}