Skip to content

Instantly share code, notes, and snippets.

@rst0git
Last active March 25, 2019 22:09
Show Gist options
  • Save rst0git/f5982eaddcf471068249ee8216920f86 to your computer and use it in GitHub Desktop.
Save rst0git/f5982eaddcf471068249ee8216920f86 to your computer and use it in GitHub Desktop.
#include "main.h"
#include <errno.h>
int main(int argc, char *argv[])
{
unsigned long long len;
unsigned long long ret = -1;
int fd;
char *buf;
char buf_args[50];
clock_t begin, end;
double time_spent;
struct iovec local[1], remote[1];
pid_t rpid;
fd = open(NAME, O_RDONLY);
if (fd < 0) {
printf("Failed to open "NAME"\n");
exit(1);
}
read(fd, buf_args, 50);
close(fd);
if ((sscanf(buf_args, "%lld %p %d",
&len, &(remote->iov_base), &rpid)) != 3) {
printf("Invalid args\n");
exit(1);
}
buf = malloc(len * sizeof(char));
if (buf == NULL) {
printf("No memory: %lld\n", len);
goto out;
}
local->iov_base = buf;
local->iov_len = len;
remote->iov_len = len;
begin = clock();
while(local->iov_len !=0 &&
(ret = process_vm_readv(rpid, local, 1, remote, 1, 0)) != 0) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror("process_vm_readv");
goto out;
}
local->iov_base += ret;
remote->iov_base += ret;
local->iov_len -= ret;
remote->iov_len -= ret;
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("len = %s\tread time:\t%f\n", get_size(len), time_spent);
ret = 0;
out:
kill(rpid, SIGKILL);
unlink(NAME);
return ret;
}
#define _GNU_SOURCE
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/stat.h>
#define NAME "testfifo"
char *get_size(size_t size)
{
static const char *SIZES[] = {"B", "kB", "MB", "GB"};
size_t div = 0;
size_t rem = 0;
char *buf;
buf = malloc(50 * sizeof(char));
while (size >= 1024 &&
div < (sizeof(SIZES)/sizeof(*SIZES))) {
rem = (size % 1024);
div++;
size /= 1024;
}
snprintf(buf, 50, "%.1f %s",
(float)size + (float)rem / 1024.0,
SIZES[div]);
return buf;
}
all: build run
build:
gcc -Wall dst.c -o dst
gcc -Wall src.c -o src
run: t1 t2 t3 t4 t5 t6 t7 t8
cat *.log >> results.txt
rm -f *.log
t1:
setsid ./src 1 &
sleep 1
sudo ./dst >> t1.log
t2:
setsid ./src 2 &
sleep 1
sudo ./dst >> t2.log
t3:
setsid ./src 3 &
sleep 1
sudo ./dst >> t3.log
t4:
setsid ./src 4 &
sleep 1
sudo ./dst >> t4.log
t5:
setsid ./src 5 &
sleep 1
sudo ./dst >> t5.log
t6:
setsid ./src 6 &
sleep 1
sudo ./dst >> t6.log
t7:
setsid ./src 7 &
sleep 1
sudo ./dst >> t7.log
t8:
setsid ./src 8 &
sleep 1
sudo ./dst >> t8.log
clean:
rm -f src dst *.log
len = 256.0 MB read time: 0.057027
len = 512.0 MB read time: 0.112965
len = 768.0 MB read time: 0.169697
len = 1.0 GB read time: 0.224373
len = 1.2 GB read time: 0.278507
len = 1.5 GB read time: 0.333400
len = 1.8 GB read time: 0.393584
len = 2.0 GB read time: 0.459082
#include "main.h"
int main(int argc, char *argv[])
{
unsigned long long len = 256 * 1024 * 1024;
int fd;
char dst_args[50];
unsigned dst_args_len;
char *buf;
if (argc != 2) {
printf("Usage: %s <BUFFER_LENGTH>\n", argv[0]);
exit(1);
}
unlink(NAME);
mkfifo(NAME, 0666);
len *= atoi(argv[1]);
buf = malloc(len * sizeof(char));
dst_args_len = sprintf(dst_args, "%lld %p %d\n", len, buf, getpid());
fd = open(NAME, O_WRONLY);
write(fd, dst_args, dst_args_len);
close(fd);
for (int i = 0; i < len; ++i)
buf[i] = (i % 26) + 'A';
buf[len] = '\0';
while(1)
sleep(1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment