核心代码
buildmyram.c
#include <minix/paths.h>
#include <sys/ioc_memory.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
int fd;
signed long size;
char* d;
if (argc < 2 || argc > 3) {
fprintf(stderr, "usage: %s <size in MB> [device]\n",argv[0]);
return 1;
}
d = argc == 2 ? _PATH_RAMDISK : argv[2];
if ((fd = open(d, O_RDONLY)) < 0) {
perror(d);
return 1;
}
#define MFACTOR 1048576
size = atol(argv[1]) * MFACTOR;
if (size < 0) {
fprintf(stderr, "size should be non-negative.\n");
return 1;
}
if (ioctl(fd, MIOCRAMSIZE, &size) < 0) {
perror("MIOCRAMSIZE");
return 1;
}
fprintf(stderr, "size on %s set to %ldMB\n", d, size / MFACTOR);
return 0;
}
change.txt
diff --git a/minix/drivers/storage/memory/memory.c b/minix/drivers/storage/memory/memory.c
index fb5aeaa..9acb114 100644
--- a/minix/drivers/storage/memory/memory.c
+++ b/minix/drivers/storage/memory/memory.c
@@ -34,7 +34,7 @@
#include "local.h"
/* ramdisks (/dev/ram*) */
-#define RAMDISKS 6
+#define RAMDISKS 7
#define RAM_DEV_LAST (RAM_DEV_FIRST+RAMDISKS-1)
debug.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<time.h>
#include<string.h>
#define MAXWRITEBUF (1024*1024) //写缓冲最大容量
char write_buff[MAXWRITEBUF];
char RAMpath[100][64];
char rambase[] ="/root/myram/test",endtmp[]=".txt";
void Solve_Filename(){
char test[120][4]={"1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31","32","33","34","35","36","37","38","39","40",
"41","42","43","44","45","46","47","48","49","50",
"51","52","53","54","55","56","57","58","59","60",
"61","62","63","64","65","66","67","68","69","70",
"71","72","73","74","75","76","77","78","79","80",
"81","82","83","84","85","86","87","88","89","90",
"91","92","93","94","95","96","97","98","99"};
for(int i=1;i<=99;i++){
strcpy(RAMpath[i],rambase);
strcat(RAMpath[i],test[i]);
strcat(RAMpath[i],endtmp);
}
return;
}
int main(){
Solve_Filename();
for(int i=1;i<=99;i++)printf("%s\n",RAMpath[i]);
return 0;
}
run.sh
clang test.c -o test
./test W R R
echo GET RAMrandWRITE
./test R R R
echo GET RAMrandREAD
rm /root/myram/*.txt
./test W R D
echo GET DISKrandWRITE
./test R R D
echo GET DISKrandREAD
rm /usr/*.txt
./test W O R
echo GET RAMorderWRITE
./test R O R
echo GET RAMorderREAD
rm /root/myram/*.txt
./test W O D
echo GET DISKorderWRITE
./test R O D
echo GET DISKorderREAD
rm /usr/*.txt
test.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<time.h>
#include<string.h>
#include<errno.h>
#define MAXITER 1000
#define MAXWRITEBUF (1024*1024) //写缓冲最大容量
#define MAXREADBUF (1024*1024) //读缓冲最大容量
#define MAX_FILESIZE (300*1024*1024) //大块 300MB最大
#define CONCURRENT 8 //并发数
char RAMpath[32][64],DISKpath[32][64];
char rambase[] ="/root/myram/test",diskbase[]="/usr/test",endtmp[]=".txt";
char write_buff[MAXWRITEBUF];
char read_buff[MAXREADBUF];
void read_file(int blocksize, int isrand, char* filepath){
int fd = open(filepath,O_RDWR|O_SYNC|O_CREAT,0755);
if (fd == -1) {
fprintf(stderr, "FILE OPEN ERROR\n");
return;
}
for (int i=0;i<MAXITER;i++) {
if (read(fd,read_buff,blocksize) != blocksize) {
fprintf(stderr, "FILE READ ERROR\n");
return;
}
if (isrand) {
lseek(fd,(MAXITER-1)*(rand() % blocksize),SEEK_SET);
}
}
lseek(fd, 0, SEEK_SET);
}
void write_file(int blocksize, int isrand, char* filepath) {
int fd = open(filepath,O_RDWR|O_SYNC|O_CREAT,0755);
if (fd == -1){
fprintf(stderr, "FILE OPEN ERROR\n");
return;
}
for (int i=0;i<MAXITER;i++) {
if(write(fd,write_buff,blocksize)!= blocksize){
fprintf(stderr,"FILE WRITE ERROR\n");
return;
}
if(isrand){
lseek(fd,rand() % (MAX_FILESIZE-blocksize),SEEK_SET);
}
}
lseek(fd, 0, SEEK_SET);
}
void Solve_Filename(){
char test[20][3]={"0","1","2","3","4","5","6","7","8",
"9","10","11","12","13","14","15","16"};
for(int i=1;i<=16;i++){
strcpy(RAMpath[i],rambase);
strcat(RAMpath[i],test[i]);
strcat(RAMpath[i],endtmp);
strcpy(DISKpath[i],diskbase);
strcat(DISKpath[i],test[i]);
strcat(DISKpath[i],endtmp);
}
return;
}
double calc_time(struct timeval t1, struct timeval t2) {
return (double)(t2.tv_sec - t1.tv_sec)*1000+(t2.tv_usec-t1.tv_usec)/1000;
}
int main(int argc, char* argv[]){
srand((unsigned)time(NULL));
Solve_Filename();
double Time;
struct timeval t1,t2;
for (int i=0;i<MAXWRITEBUF;i+=26)strcat(write_buff,"abcdefghijklmnopqrstuvwxyz");
printf("BlockSize(KB),Speed(MB/s)\n");
for(int blocksize=64;blocksize<=64*1024;blocksize*=2){
gettimeofday(&t1,NULL);
for(int i=0;i<CONCURRENT;i++){
if(fork()==0){
if(!strcmp(argv[1],"W")){
//写
if(!strcmp(argv[2],"R")){
//随机
if(!strcmp(argv[3],"R")){
//ram盘
write_file(blocksize,1,RAMpath[i]);
}else{
//磁盘
write_file(blocksize,1,DISKpath[i]);
}
}else{
//顺序
if(!strcmp(argv[3],"R")){
//ram盘
write_file(blocksize,0,RAMpath[i]);
}else{
//磁盘
write_file(blocksize,0,DISKpath[i]);
}
}
}else{
//读
if(!strcmp(argv[2],"R")){
//随机
if(!strcmp(argv[3],"R")){
//ram盘
read_file(blocksize,1,RAMpath[i]);
}else{
//磁盘
read_file(blocksize,1,DISKpath[i]);
}
}else{
//顺序
if(!strcmp(argv[3],"R")){
//ram盘
read_file(blocksize,0,RAMpath[i]);
}else{
//磁盘
read_file(blocksize,0,DISKpath[i]);
}
}
}
exit(0);
}
}
while(wait(NULL)!=-1);
gettimeofday(&t2,NULL);
Time=calc_time(t1,t2)/1000.0;
int sumsize=CONCURRENT*MAXITER*blocksize;
printf("%lf,%lf\n",((double)blocksize)/1024,((double)sumsize/Time/1024.0/1024));
}
return 0;
}
test2.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<time.h>
#include<string.h>
#include<errno.h>
#define MAXITER 1000
#define MAXWRITEBUF (1024*1024) //写缓冲最大容量
#define MAX_FILESIZE (300*1024*1024) //大块 300MB最大
#define Blocksize (8*1024)
char write_buff[MAXWRITEBUF];
char RAMpath[100][64];
char rambase[] ="/root/myram/test",endtmp[]=".txt";
void write_file(int blocksize, int isrand, char* filepath) {
int fd = open(filepath,O_RDWR|O_SYNC|O_CREAT,0755);
if (fd == -1){
fprintf(stderr, "FILE OPEN ERROR\n");
return;
}
for (int i=0;i<MAXITER;i++) {
if(write(fd,write_buff,blocksize)!= blocksize){
fprintf(stderr,"FILE WRITE ERROR\n");
return;
}
if(isrand){
lseek(fd,rand() % (MAX_FILESIZE-blocksize),SEEK_SET);
}
}
lseek(fd, 0, SEEK_SET);
}
double calc_time(struct timeval t1, struct timeval t2) {
return (double)(t2.tv_sec - t1.tv_sec)*1000+(t2.tv_usec-t1.tv_usec)/1000;
}
void Solve_Filename(){
char test[120][4]={"1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31","32","33","34","35","36","37","38","39","40",
"41","42","43","44","45","46","47","48","49","50",
"51","52","53","54","55","56","57","58","59","60",
"61","62","63","64","65","66","67","68","69","70",
"71","72","73","74","75","76","77","78","79","80",
"81","82","83","84","85","86","87","88","89","90",
"91","92","93","94","95","96","97","98","99"};
for(int i=1;i<=99;i++){
strcpy(RAMpath[i],rambase);
strcat(RAMpath[i],test[i]);
strcat(RAMpath[i],endtmp);
}
return;
}
int main(){
srand((unsigned)time(NULL));
Solve_Filename();
double Time;
struct timeval t1,t2;
for (int i=0;i<MAXWRITEBUF;i+=26)strcat(write_buff,"abcdefghijklmnopqrstuvwxyz");
for (int num=1;num<=80;num++) {
gettimeofday(&t1, NULL);
for (int i=0;i<num;i++) {
int pid=fork();
if (pid==0) {
write_file(Blocksize,0,RAMpath[i]);
exit(0);
}
}
while (wait(NULL)!=-1);
gettimeofday(&t2,NULL);
Time=calc_time(t1,t2)/1000.0;
int sumsize=num*MAXITER*Blocksize;
printf("%d,%lf,%lf\n",num,((double)sumsize/Time/1024.0/1024.0),Time);
}
return 0;
}