- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图找出在尝试并行比较和复制设备 block 与 pthreads 时我做错了什么。看起来我正在脱离同步并且比较阶段无法正常工作。任何帮助将不胜感激
#ifndef __dbg_h__
#define __dbg_h__
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#ifdef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "DEBUG %s %s %s:%d: " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
#define log_err(M, ...) fprintf(stderr, "[ERROR] %s %s (%s:%d: errno: %s) " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define log_warn(M, ...) fprintf(stderr, "[WARN] %s %s (%s:%d: errno: %s) " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define log_info(M, ...) fprintf(stderr, "[INFO] %s %s (%s:%d) " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#define blksize 8192
void *compare_devices(void *arguments);
struct arg_struct {
char device_name_a[1024];
char device_name_b[1024];
off_t start_offset;
off_t end_offset;
int thread_number;
int sub_total;
};
int main(int argc, char **argv)
{
int fd;
long numblocks=0;
int i;
int err;
long total_different_size=0;
int number_of_threads=16;
struct arg_struct compare_devices_args[number_of_threads];
fd = open(argv[1], O_RDONLY);
ioctl(fd, BLKGETSIZE, &numblocks);
close(fd);
log_info("Number of blocks: %lu, this makes %.3f GB\n",
numblocks,
(double)numblocks * 512.0 / (1024 * 1024 * 1024));
//read_whole_device(argv[1]);
long number_of_bytes_per_thread;
number_of_bytes_per_thread=numblocks*512/(long)number_of_threads;
pthread_t tid[number_of_threads];
for (i=0; i<number_of_threads; i++) {
strcpy(compare_devices_args[i].device_name_a, argv[1]);
strcpy(compare_devices_args[i].device_name_b, argv[2]);
compare_devices_args[i].start_offset=(long)(i*number_of_bytes_per_thread);
compare_devices_args[i].end_offset=(long)((i+1)*number_of_bytes_per_thread-1);
compare_devices_args[i].thread_number=i+1;
err = pthread_create(&(tid[i]), NULL, &compare_devices,(void*)&compare_devices_args[i]);
if (err != 0) {
printf("\ncan't create thread :[%s]", strerror(err));
return -1;
}
}
for (i=0; i<number_of_threads; i++) {
pthread_join(tid[i], NULL);
total_different_size+=(long)compare_devices_args[i].sub_total;
}
printf ("Total of Different size between devices - %ld\n",total_different_size);
};
int read_n_bytes_from(int fd, off_t pos, char *buf, int n)
{
if (lseek(fd, pos, SEEK_SET) >= 0)
return read(fd, buf, n);
else
return -1;
}
void read_whole_device(char* device_name)
{
int fd;
char buf[1024*1024];
size_t size;
int counter=0;
log_info("reading device %s\n",device_name);
fd = open(device_name, O_RDONLY|O_NONBLOCK);
//ssize_t size = read(fd, &buf, 1024);
lseek(fd, 0, SEEK_SET);
while ( (size=read(fd, buf, 1024*1024)) > 0 ) {
printf("Read buffer %d - %d\n", size, counter);
counter++;
}
close(fd);
}
void write_block_to_device(int fd,off_t pos,char* buf, int n)
{
if (lseek(fd, pos, SEEK_SET) >= 0) {
//write
}
}
void compare_buffer(char* buf_first,char* buf_second,int length, int blk_size, int* result)
{
int i;
char buf_cpy[blk_size];
for (i=0; i<=(length/blk_size);i++)
{
if ( memcmp(buf_first+(blk_size*i), buf_second+(blk_size*i), blk_size) != 0) {
//printf ("Block %d is different\n",i);
result[i]=1;
}
else {
result[i]=0;
}
}
}
void *compare_devices(void *arguments)
{
struct arg_struct *args = arguments;
int fd_first,fd_second;
char buf_first[1024*1024];
char buf_second[1024*104];
int size_first,size_second;
int counter=0;
int result[128];
int memcmp_result;
int i;
off_t pos,pos_first,pos_second;
int total_number_of_different_blocks,difference_in_mb;
long number_of_mb_to_scan=0;
total_number_of_different_blocks=0;
log_info("Thread %d - compare devices %s,%s - start %ld end %ld\n",args->thread_number,args->device_name_a,args->device_name_b,args->start_offset,args->end_offset);
fd_first = open(args->device_name_a, O_RDONLY);
fd_second = open(args->device_name_b, O_RDONLY);
log_info("Thread %d - %d %d\n",args->thread_number,fd_first,fd_second);
log_info("Thread %d - before lseek - %ld %ld\n",args->thread_number,lseek(fd_first, 0, SEEK_CUR),lseek(fd_second, 0, SEEK_CUR));
lseek(fd_first, (off_t)args->start_offset, SEEK_SET);
lseek(fd_second, (off_t)args->start_offset, SEEK_SET);
log_info("Thread %d - after lseek - %ld %ld\n",args->thread_number,lseek(fd_first, 0, SEEK_CUR),lseek(fd_second, 0, SEEK_CUR));
log_info("Thread %d - start %ld , %ld\n",args->thread_number,lseek(fd_first, 0, SEEK_CUR),lseek(fd_second, 0, SEEK_CUR));
number_of_mb_to_scan=(args->end_offset-args->start_offset)/1024/1024;
log_info("Thread %d - Number of MB to scan %ld - start offset %ld\n",args->thread_number,number_of_mb_to_scan,lseek(fd_second, 0, SEEK_CUR));
memset(buf_first, 0, sizeof(buf_first));
memset(buf_second, 0, sizeof(buf_second));
while ( (size_first=read(fd_first, buf_first, 1024*1024)) > 0 ) {
size_second=read(fd_second,buf_second, 1024*1024);
pos_first=lseek(fd_first, 0, SEEK_CUR);
pos_second=lseek(fd_second, 0, SEEK_CUR);
log_info("Thread %d - fd (%d,%d) pos_first %lld, pos_second %lld (%d - %d) - read %d,%d\n",args->thread_number,fd_first,fd_second,(unsigned long long)pos_first-1024*1024,(unsigned long long)pos_second-1024*1024,sizeof(off_t),sizeof(pos),size_first,size_second);
//log_info("Thread %s - Hash Buf A - %ld Hash Buf B - %ld",args->thread_number,adler32(buf_first,size_first),adler32(buf_second,size_second));
if ( (memcmp_result=memcmp(buf_first, buf_second, sizeof(buf_second))) != 0 ) {
log_info("Thread %d - Found 1MB chunck which is different at pos %lld - %d (%d)\n",args->thread_number,(unsigned long long)pos_first-1024*1024, memcmp_result,sizeof(buf_first));
compare_buffer(buf_first,buf_second,1024*1024,blksize,result);
for (i=0; i<=128; i++) {
if ( result[i] == 1 ) {
//printf ("%d,",result[i]);
total_number_of_different_blocks++;
}
}
}
if ( pos_first > args->end_offset ) {
break;
}
memset(buf_first, 0, sizeof(buf_first));
memset(buf_second, 0, sizeof(buf_second));
counter++;
}
log_info("Thread %d - Number of MB to scan %ld - end offset %ld\n",args->thread_number,number_of_mb_to_scan,lseek(fd_first, 0, SEEK_CUR));
log_info("Thread %d - Completed - Scanning %d of 1MB chuncks\n",args->thread_number,counter);
log_info("Thread %d - Diffence of size between %s to %s starting at %ld and ending at %ld- %d different blocks of 8k - %dMB\n",args->thread_number,args->device_name_a,args->device_name_b,args->start_offset,args->end_offset,total_number_of_different_blocks,total_number_of_different_blocks*8192/1024/1024);
args->sub_total=total_number_of_different_blocks/8/1024;
close(fd_first);
close(fd_second);
pthread_exit(arguments);
}
更新版本:
#ifndef __dbg_h__
#define __dbg_h__
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <getopt.h>
#include <openssl/sha.h>
#include <sched.h>
#ifdef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "DEBUG %s %s %s:%d: " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
#define log_err(M, ...) fprintf(stderr, "[ERROR] %s %s (%s:%d: errno: %s) " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, clean_er
rno(), ##__VA_ARGS__)
#define log_warn(M, ...) fprintf(stderr, "[WARN] %s %s (%s:%d: errno: %s) " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, clean_er
rno(), ##__VA_ARGS__)
#define log_info(M, ...) fprintf(stderr, "[INFO] %s %s (%s:%d) " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#ifndef _TRY_THROW_CATCH_H_
#define _TRY_THROW_CATCH_H_
#include <setjmp.h>
#define TRY do { jmp_buf ex_buf__; switch( setjmp(ex_buf__) ) { case 0: while(1) {
#define CATCH(x) break; case x:
#define FINALLY break; } default: {
#define ETRY break; } } }while(0)
#define THROW(x) longjmp(ex_buf__, x)
#endif /*!_TRY_THROW_CATCH_H_*/
unsigned long long chunck_size=1024*1024;
int blksize=16384;
int cb=0;
void *compare_devices(void *arguments);
struct arg_struct {
char device_name_a[1024];
char device_name_b[1024];
off64_t start_offset;
off64_t end_offset;
int thread_number;
int sub_total;
};
int main(int argc, char **argv)
{
int fd,s_is_set=0,d_is_set=0,copy_blocks=0,num_cpus;
int opt;
char *source_device,*dest_device,*blksize_string,*number_of_threads_string;
long numblocks_src=0,numblocks_dst=0;
int i;
int err,policy;
long total_different_size=0;
int number_of_threads=16;
int newprio, set_thread_priority=0;
pthread_attr_t tattr;
struct sched_param param;
while ((opt = getopt(argc, argv, "s:d:b:p:t:c")) != -1) {
switch (opt) {
case 's':
source_device = optarg;
log_info("source device is %s\n",source_device);
s_is_set=1;
if( access( source_device, F_OK ) != -1 ) {
log_info("source device is accessible\n");
}
else {
log_err("cannot access source device\n");
return -1;
}
break;
case 'd':
dest_device = optarg;
log_info("dest device is %s\n",dest_device);
d_is_set=1;
if( access( dest_device, F_OK ) != -1 ) {
log_info("dest device is accessible\n");
}
else {
log_err("cannot access dest device\n");
return -1;
}
break;
case 'b':
blksize = atoi(optarg);
if (blksize%4096 != 0) {
log_err("Block Size is not multiple of 4k\n");
return -1;
}
else
log_info("block size is %d\n",blksize);
break;
case 't':
number_of_threads = atoi(optarg);
num_cpus = sysconf( _SC_NPROCESSORS_ONLN );
if ( number_of_threads%2 != 0 ) {
log_err("Number of threads are not multiple of 2\n");
return -1;
}
else
log_info("number of threads is %d - number of cores %d\n",number_of_threads,num_cpus);
if ( number_of_threads > num_cpus ) {
number_of_threads=num_cpus;
}
break;
case 'c':
copy_blocks=1;
cb=1;
break;
case 'p':
if ( optarg != NULL ) {
newprio=atoi(optarg);
set_thread_priority=1;
}
break;
case ':':
log_info("%s: option '-%c' requires an argument\n",argv[0], optopt);
break;
case '?':
default:
log_info("%s: option '-%c' is invalid: ignored\n",argv[0], optopt);
break;
}
}
if ( s_is_set != 1 || d_is_set !=1 ) {
log_err("Source device and Destination device must be specified\n");
return -1;
}
struct arg_struct compare_devices_args[number_of_threads];
fd = open(source_device, O_RDONLY);
ioctl(fd, BLKGETSIZE, &numblocks_src);
close(fd);
log_info("Number of blocks in source device: %lu, this makes %.3f GB\n", numblocks_src, (double)numblocks_src * 512.0 / (1024 * 102
4 * 1024));
fd = open(dest_device, O_RDONLY);
ioctl(fd, BLKGETSIZE, &numblocks_dst);
close(fd);
log_info("Number of blocks in destination is device: %lu, this makes %.3f GB\n", numblocks_dst, (double)numblocks_dst * 512.0 / (10
24 * 1024 * 1024));
if ( numblocks_src > numblocks_dst ) {
log_info("Number of blocks in source device is larger then number of blocks in destination device");
return -1;
}
long number_of_bytes_per_thread;
number_of_bytes_per_thread=numblocks_src*512/(long)number_of_threads;
pthread_t tid[number_of_threads];
for (i=0; i<number_of_threads; i++) {
strcpy(compare_devices_args[i].device_name_a, source_device);
strcpy(compare_devices_args[i].device_name_b, dest_device);
compare_devices_args[i].start_offset=(long)(i*number_of_bytes_per_thread);
compare_devices_args[i].end_offset=(long)((i+1)*number_of_bytes_per_thread-1);
compare_devices_args[i].thread_number=i+1;
if ( set_thread_priority == 1 ) {
err = pthread_attr_init (&tattr);
policy = SCHED_RR;
err = pthread_attr_setschedpolicy(&tattr, policy);
if (err != 0) {
log_err("\nThread %d - Unable to set scheduler policy attributes :[%s]", i+1, strerror(err));
}
err = pthread_attr_getschedparam (&tattr, ¶m);
if (err != 0) {
log_err("\nThread %d - Unable to get scheduler priority attributes :[%s]", i+1, strerror(err));
}
log_info("\nThread %d - Old thread priority is %d",i+1,param.sched_priority);
param.sched_priority = newprio;
log_info("\nThread %d - Setting pthread priority to %d",i+1,newprio);
err = pthread_attr_setschedparam (&tattr, ¶m);
if (err != 0) {
log_err("\nThread %d - Unable to set scheduler attributes :[%s]", i+1, strerror(err));
}
err = pthread_create(&(tid[i]), &tattr, &compare_devices,(void*)&compare_devices_args[i]);
}
else
{
err = pthread_create(&(tid[i]), NULL, &compare_devices,(void*)&compare_devices_args[i]);
}
if (err != 0) {
log_err("\nThread %d - can't create thread :[%s]", i+1, strerror(err));
return -1;
}
}
for (i=0; i<number_of_threads; i++) {
pthread_join(tid[i], NULL);
total_different_size+=(long)compare_devices_args[i].sub_total;
}
log_info ("Total of Different size between devices - %ld\n",total_different_size);
};
void compare_buffer(char* buf_first,char* buf_second,int length, int blk_size, int* result)
{
int i;
char buf_cpy[blk_size];
for (i=0; i<=(length/blk_size);i++)
{
if ( memcmp(buf_first+(blk_size*i), buf_second+(blk_size*i), blk_size) != 0) {
result[i]=1;
}
else {
result[i]=0;
}
}
}
void *compare_devices(void *arguments)
{
struct arg_struct *args = arguments;
int fd_first,fd_second;
char buf_first[chunck_size];
char buf_second[chunck_size];
int size_first,size_second;
unsigned int counter=0;
int result[chunck_size/blksize];
int memcmp_result;
unsigned long long i;
off64_t pos,pos_first,pos_second;
unsigned long long total_number_of_different_blocks,difference_in_mb;
long number_of_mb_to_scan=0;
total_number_of_different_blocks=0;
log_info("Thread %d - compare devices %s,%s - start %llu end %llu\n",args->thread_number,args->device_name_a,args->device_nam
e_b,args->start_offset,args->end_offset);
fd_first = open(args->device_name_a, O_RDONLY);
if ( cb == 0 ) {
fd_second = open(args->device_name_b, O_RDONLY);
}
else
fd_second = open(args->device_name_b, O_RDWR);
lseek64(fd_first, (off64_t)args->start_offset, SEEK_SET);
lseek64(fd_second, (off64_t)args->start_offset, SEEK_SET);
number_of_mb_to_scan=(args->end_offset-args->start_offset)/chunck_size;
debug("Thread %d - Number of MB to scan %ld - start offset %ld\n",args->thread_number,number_of_mb_to_scan,lseek64(fd_second,
0, SEEK_CUR));
memset(buf_first, 0, sizeof(buf_first));
memset(buf_second, 0, sizeof(buf_second));
while ( (size_first=read(fd_first, buf_first, chunck_size)) > 0 && counter <= number_of_mb_to_scan ) {
size_second=read(fd_second,buf_second, chunck_size);
pos_first=lseek64(fd_first, 0, SEEK_CUR);
pos_second=lseek64(fd_second, 0, SEEK_CUR);
if ( (memcmp_result=memcmp(buf_first, buf_second, sizeof(buf_second))) != 0 ) {
debug("Thread %d - Found 1MB chunck which is different at pos %llu - %d (%d)\n",args->thread_number,(unsigned
long long)pos_first-chunck_size, memcmp_result,sizeof(buf_first));
compare_buffer(buf_first,buf_second,chunck_size,blksize,result);
for (i=0; i<=chunck_size/blksize; i++) {
if ( result[i] == 1 ) {
if ( cb == 1 ) {
debug("Thread %d - chunck %d - copying block %d - location of block in device file %llu",
args->thread_number, counter+1, i, args->start_offset+(unsigned long long)(chunck_size*counter)+(unsigned long long)(i*blksize));
lseek64(fd_first,args->start_offset+(off64_t)(chunck_size*counter)+(off64_t)(i*blksize),S
EEK_SET);
lseek64(fd_second,args->start_offset+(off64_t)(chunck_size*counter)+(off64_t)(i*blksize),
SEEK_SET);
size_first=read(fd_first, buf_first, blksize);
size_second=write(fd_second,buf_first,size_first);
if ( size_second != blksize ) {
log_err("Thread %d - Houston we have a problem in copying block number %llu",args->th
read_number, (unsigned long long)args->start_offset+(unsigned long long)(chunck_size*counter)+(unsigned long long)(i*blksize));
}
}
total_number_of_different_blocks++;
}
}
}
if ( cb == 1) {
lseek64(fd_second,(off64_t)args->start_offset+(off64_t)(chunck_size*(counter+1)),SEEK_SET);
lseek64(fd_first,(off64_t)args->start_offset+(off64_t)(chunck_size*(counter+1)),SEEK_SET);
}
if ( pos_first > args->end_offset ) {
break;
}
memset(buf_first, 0, sizeof(buf_first));
memset(buf_second, 0, sizeof(buf_second));
debug("Thread %d - chunck %d out of %d done - %llu %llu", args->thread_number, counter+1, number_of_mb_to_scan,pos_fi
rst,args->end_offset);
counter++;
}
log_info("Thread %d - Number of MB to scan %ld - end offset %llu\n",args->thread_number,number_of_mb_to_scan,lseek64(fd_first
, 0, SEEK_CUR));
log_info("Thread %d - Completed - Scanning %d of 1MB chuncks\n",args->thread_number,counter);
log_info("Thread %d - Difference of size between %s to %s starting at %llu and ending at %llu- %d different blocks of %d - %d
MB\n",args->thread_number,args->device_name_a,args->device_name_b,args->start_offset,args->end_offset,total_number_of_different_block
s,blksize,total_number_of_different_blocks*blksize/chunck_size);
args->sub_total=total_number_of_different_blocks*blksize/1024;
close(fd_first);
close(fd_second);
pthread_exit(arguments);
}
最佳答案
#ifndef __dbg_h__
#define __dbg_h__
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <getopt.h>
#include <openssl/sha.h>
#include <sched.h>
#ifdef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "DEBUG %s %s %s:%d: " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
#define log_err(M, ...) fprintf(stderr, "[ERROR] %s %s (%s:%d: errno: %s) " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, clean_er
rno(), ##__VA_ARGS__)
#define log_warn(M, ...) fprintf(stderr, "[WARN] %s %s (%s:%d: errno: %s) " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, clean_er
rno(), ##__VA_ARGS__)
#define log_info(M, ...) fprintf(stderr, "[INFO] %s %s (%s:%d) " M "\n", __DATE__, __TIME__, __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#ifndef _TRY_THROW_CATCH_H_
#define _TRY_THROW_CATCH_H_
#include <setjmp.h>
#define TRY do { jmp_buf ex_buf__; switch( setjmp(ex_buf__) ) { case 0: while(1) {
#define CATCH(x) break; case x:
#define FINALLY break; } default: {
#define ETRY break; } } }while(0)
#define THROW(x) longjmp(ex_buf__, x)
#endif /*!_TRY_THROW_CATCH_H_*/
unsigned long long chunck_size=1024*1024;
int blksize=16384;
int cb=0;
void *compare_devices(void *arguments);
struct arg_struct {
char device_name_a[1024];
char device_name_b[1024];
off64_t start_offset;
off64_t end_offset;
int thread_number;
int sub_total;
};
int main(int argc, char **argv)
{
int fd,s_is_set=0,d_is_set=0,copy_blocks=0,num_cpus;
int opt;
char *source_device,*dest_device,*blksize_string,*number_of_threads_string;
long numblocks_src=0,numblocks_dst=0;
int i;
int err,policy;
long total_different_size=0;
int number_of_threads=16;
int newprio, set_thread_priority=0;
pthread_attr_t tattr;
struct sched_param param;
while ((opt = getopt(argc, argv, "s:d:b:p:t:c")) != -1) {
switch (opt) {
case 's':
source_device = optarg;
log_info("source device is %s\n",source_device);
s_is_set=1;
if( access( source_device, F_OK ) != -1 ) {
log_info("source device is accessible\n");
}
else {
log_err("cannot access source device\n");
return -1;
}
break;
case 'd':
dest_device = optarg;
log_info("dest device is %s\n",dest_device);
d_is_set=1;
if( access( dest_device, F_OK ) != -1 ) {
log_info("dest device is accessible\n");
}
else {
log_err("cannot access dest device\n");
return -1;
}
break;
case 'b':
blksize = atoi(optarg);
if (blksize%4096 != 0) {
log_err("Block Size is not multiple of 4k\n");
return -1;
}
else
log_info("block size is %d\n",blksize);
break;
case 't':
number_of_threads = atoi(optarg);
num_cpus = sysconf( _SC_NPROCESSORS_ONLN );
if ( number_of_threads%2 != 0 ) {
log_err("Number of threads are not multiple of 2\n");
return -1;
}
else
log_info("number of threads is %d - number of cores %d\n",number_of_threads,num_cpus);
if ( number_of_threads > num_cpus ) {
number_of_threads=num_cpus;
}
break;
case 'c':
copy_blocks=1;
cb=1;
break;
case 'p':
if ( optarg != NULL ) {
newprio=atoi(optarg);
set_thread_priority=1;
}
break;
case ':':
log_info("%s: option '-%c' requires an argument\n",argv[0], optopt);
break;
case '?':
default:
log_info("%s: option '-%c' is invalid: ignored\n",argv[0], optopt);
break;
}
}
if ( s_is_set != 1 || d_is_set !=1 ) {
log_err("Source device and Destination device must be specified\n");
return -1;
}
struct arg_struct compare_devices_args[number_of_threads];
fd = open(source_device, O_RDONLY);
ioctl(fd, BLKGETSIZE, &numblocks_src);
close(fd);
log_info("Number of blocks in source device: %lu, this makes %.3f GB\n", numblocks_src, (double)numblocks_src * 512.0 / (1024 * 102
4 * 1024));
fd = open(dest_device, O_RDONLY);
ioctl(fd, BLKGETSIZE, &numblocks_dst);
close(fd);
log_info("Number of blocks in destination is device: %lu, this makes %.3f GB\n", numblocks_dst, (double)numblocks_dst * 512.0 / (10
24 * 1024 * 1024));
if ( numblocks_src > numblocks_dst ) {
log_info("Number of blocks in source device is larger then number of blocks in destination device");
return -1;
}
long number_of_bytes_per_thread;
number_of_bytes_per_thread=numblocks_src*512/(long)number_of_threads;
pthread_t tid[number_of_threads];
for (i=0; i<number_of_threads; i++) {
strcpy(compare_devices_args[i].device_name_a, source_device);
strcpy(compare_devices_args[i].device_name_b, dest_device);
compare_devices_args[i].start_offset=(long)(i*number_of_bytes_per_thread);
compare_devices_args[i].end_offset=(long)((i+1)*number_of_bytes_per_thread-1);
compare_devices_args[i].thread_number=i+1;
if ( set_thread_priority == 1 ) {
err = pthread_attr_init (&tattr);
policy = SCHED_RR;
err = pthread_attr_setschedpolicy(&tattr, policy);
if (err != 0) {
log_err("\nThread %d - Unable to set scheduler policy attributes :[%s]", i+1, strerror(err));
}
err = pthread_attr_getschedparam (&tattr, ¶m);
if (err != 0) {
log_err("\nThread %d - Unable to get scheduler priority attributes :[%s]", i+1, strerror(err));
}
log_info("\nThread %d - Old thread priority is %d",i+1,param.sched_priority);
param.sched_priority = newprio;
log_info("\nThread %d - Setting pthread priority to %d",i+1,newprio);
err = pthread_attr_setschedparam (&tattr, ¶m);
if (err != 0) {
log_err("\nThread %d - Unable to set scheduler attributes :[%s]", i+1, strerror(err));
}
err = pthread_create(&(tid[i]), &tattr, &compare_devices,(void*)&compare_devices_args[i]);
}
else
{
err = pthread_create(&(tid[i]), NULL, &compare_devices,(void*)&compare_devices_args[i]);
}
if (err != 0) {
log_err("\nThread %d - can't create thread :[%s]", i+1, strerror(err));
return -1;
}
}
for (i=0; i<number_of_threads; i++) {
pthread_join(tid[i], NULL);
total_different_size+=(long)compare_devices_args[i].sub_total;
}
log_info ("Total of Different size between devices - %ld\n",total_different_size);
};
void compare_buffer(char* buf_first,char* buf_second,int length, int blk_size, int* result)
{
int i;
char buf_cpy[blk_size];
for (i=0; i<=(length/blk_size);i++)
{
if ( memcmp(buf_first+(blk_size*i), buf_second+(blk_size*i), blk_size) != 0) {
result[i]=1;
}
else {
result[i]=0;
}
}
}
void *compare_devices(void *arguments)
{
struct arg_struct *args = arguments;
int fd_first,fd_second;
char buf_first[chunck_size];
char buf_second[chunck_size];
int size_first,size_second;
unsigned int counter=0;
int result[chunck_size/blksize];
int memcmp_result;
unsigned long long i;
off64_t pos,pos_first,pos_second;
unsigned long long total_number_of_different_blocks,difference_in_mb;
long number_of_mb_to_scan=0;
total_number_of_different_blocks=0;
log_info("Thread %d - compare devices %s,%s - start %llu end %llu\n",args->thread_number,args->device_name_a,args->device_nam
e_b,args->start_offset,args->end_offset);
fd_first = open(args->device_name_a, O_RDONLY);
if ( cb == 0 ) {
fd_second = open(args->device_name_b, O_RDONLY);
}
else
fd_second = open(args->device_name_b, O_RDWR);
lseek64(fd_first, (off64_t)args->start_offset, SEEK_SET);
lseek64(fd_second, (off64_t)args->start_offset, SEEK_SET);
number_of_mb_to_scan=(args->end_offset-args->start_offset)/chunck_size;
debug("Thread %d - Number of MB to scan %ld - start offset %ld\n",args->thread_number,number_of_mb_to_scan,lseek64(fd_second,
0, SEEK_CUR));
memset(buf_first, 0, sizeof(buf_first));
memset(buf_second, 0, sizeof(buf_second));
while ( (size_first=read(fd_first, buf_first, chunck_size)) > 0 && counter <= number_of_mb_to_scan ) {
size_second=read(fd_second,buf_second, chunck_size);
pos_first=lseek64(fd_first, 0, SEEK_CUR);
pos_second=lseek64(fd_second, 0, SEEK_CUR);
if ( (memcmp_result=memcmp(buf_first, buf_second, sizeof(buf_second))) != 0 ) {
debug("Thread %d - Found 1MB chunck which is different at pos %llu - %d (%d)\n",args->thread_number,(unsigned
long long)pos_first-chunck_size, memcmp_result,sizeof(buf_first));
compare_buffer(buf_first,buf_second,chunck_size,blksize,result);
for (i=0; i<=chunck_size/blksize; i++) {
if ( result[i] == 1 ) {
if ( cb == 1 ) {
debug("Thread %d - chunck %d - copying block %d - location of block in device file %llu",
args->thread_number, counter+1, i, args->start_offset+(unsigned long long)(chunck_size*counter)+(unsigned long long)(i*blksize));
lseek64(fd_first,args->start_offset+(off64_t)(chunck_size*counter)+(off64_t)(i*blksize),S
EEK_SET);
lseek64(fd_second,args->start_offset+(off64_t)(chunck_size*counter)+(off64_t)(i*blksize),
SEEK_SET);
size_first=read(fd_first, buf_first, blksize);
size_second=write(fd_second,buf_first,size_first);
if ( size_second != blksize ) {
log_err("Thread %d - Houston we have a problem in copying block number %llu",args->th
read_number, (unsigned long long)args->start_offset+(unsigned long long)(chunck_size*counter)+(unsigned long long)(i*blksize));
}
}
total_number_of_different_blocks++;
}
}
}
if ( cb == 1) {
lseek64(fd_second,(off64_t)args->start_offset+(off64_t)(chunck_size*(counter+1)),SEEK_SET);
lseek64(fd_first,(off64_t)args->start_offset+(off64_t)(chunck_size*(counter+1)),SEEK_SET);
}
if ( pos_first > args->end_offset ) {
break;
}
memset(buf_first, 0, sizeof(buf_first));
memset(buf_second, 0, sizeof(buf_second));
debug("Thread %d - chunck %d out of %d done - %llu %llu", args->thread_number, counter+1, number_of_mb_to_scan,pos_fi
rst,args->end_offset);
counter++;
}
log_info("Thread %d - Number of MB to scan %ld - end offset %llu\n",args->thread_number,number_of_mb_to_scan,lseek64(fd_first
, 0, SEEK_CUR));
log_info("Thread %d - Completed - Scanning %d of 1MB chuncks\n",args->thread_number,counter);
log_info("Thread %d - Difference of size between %s to %s starting at %llu and ending at %llu- %d different blocks of %d - %d
MB\n",args->thread_number,args->device_name_a,args->device_name_b,args->start_offset,args->end_offset,total_number_of_different_block
s,blksize,total_number_of_different_blocks*blksize/chunck_size);
args->sub_total=total_number_of_different_blocks*blksize/1024;
close(fd_first);
close(fd_second);
pthread_exit(arguments);
}
关于多线程复制 block 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39942176/
我将 Bootstrap 与 css 和 java 脚本结合使用。在不影响前端代码的情况下,我真的很难在css中绘制这个背景。在许多问题中,人们将宽度和高度设置为 0%。但是由于我的导航栏,我不能使用
我正在用 c 编写一个程序来读取文件的内容。代码如下: #include void main() { char line[90]; while(scanf("%79[^\
我想使用 javascript 获取矩阵数组的所有对 Angular 线。假设输入输出如下: input = [ [1,2,3], [4,5,6], [7,8,9], ] output =
可以用pdfmake绘制lines,circles和other shapes吗?如果是,是否有documentation或样本?我想用jsPDF替换pdfmake。 最佳答案 是的,有可能。 pdfm
我有一个小svg小部件,其目的是显示角度列表(参见图片)。 现在,角度是线元素,仅具有笔触,没有填充。但是现在我想使用一种“内部填充”颜色和一种“笔触/边框”颜色。我猜想line元素不能解决这个问题,
我正在为带有三角对象的 3D 场景编写一个非常基本的光线转换器,一切都工作正常,直到我决定尝试从场景原点 (0/0/0) 以外的点转换光线。 但是,当我将光线原点更改为 (0/1/0) 时,相交测试突
这个问题已经有答案了: Why do people write "#!/usr/bin/env python" on the first line of a Python script? (22 个回
如何使用大约 50 个星号 * 并使用 for 循环绘制一条水平线?当我尝试这样做时,结果是垂直(而不是水平)列出 50 个星号。 public void drawAstline() { f
这是一个让球以对角线方式下降的 UI,但球保持静止;线程似乎无法正常工作。你能告诉我如何让球移动吗? 请下载一个球并更改目录,以便程序可以找到您的球的分配位置。没有必要下载足球场,但如果您愿意,也可以
我在我的一个项目中使用 Jmeter 和 Ant,当我们生成报告时,它会在报告中显示 URL、#Samples、失败、成功率、平均时间、最短时间、最长时间。 我也想在报告中包含 90% 的时间线。 现
我有一个不寻常的问题,希望有人能帮助我。我想用 Canvas (android) 画一条 Swing 或波浪线,但我不知道该怎么做。它将成为蝌蚪的尾部,所以理想情况下我希望它的形状更像三角形,一端更大
这个问题已经有答案了: Checking Collision of Shapes with JavaFX (1 个回答) 已关闭 8 年前。 我正在使用 JavaFx 8 库。 我的任务很简单:我想检
如何按编号的百分比拆分文件。行数? 假设我想将我的文件分成 3 个部分(60%/20%/20% 部分),我可以手动执行此操作,-_-: $ wc -l brown.txt 57339 brown.tx
我正在努力实现这样的目标: 但这就是我设法做到的。 你能帮我实现预期的结果吗? 更新: 如果我删除 bootstrap.css 依赖项,问题就会消失。我怎样才能让它与 Bootstrap 一起工作?
我目前正在构建一个网站,但遇到了 transform: scale 的问题。我有一个按钮,当用户将鼠标悬停在它上面时,会发生两件事: 背景以对 Angular 线“扫过” 按钮标签颜色改变 按钮稍微变
我需要使用直线和仿射变换绘制大量数据点的图形(缩放图形以适合 View )。 目前,我正在使用 NSBezierPath,但我认为它效率很低(因为点在绘制之前被复制到贝塞尔路径)。通过将我的数据切割成
我正在使用基于 SVM 分类的 HOG 特征检测器。我可以成功提取车牌,但提取的车牌除了车牌号外还有一些不必要的像素/线。我的图像处理流程如下: 在灰度图像上应用 HOG 检测器 裁剪检测到的区域 调
我有以下图片: 我想填充它的轮廓(即我想在这张图片中填充线条)。 我尝试了形态学闭合,但使用大小为 3x3 的矩形内核和 10 迭代并没有填满整个边界。我还尝试了一个 21x21 内核和 1 迭代,但
我必须找到一种算法,可以找到两组数组之间的交集总数,而其中一个数组已排序。 举个例子,我们有这两个数组,我们向相应的数字画直线。 这两个数组为我们提供了总共 7 个交集。 有什么样的算法可以帮助我解决
简单地说 - 我想使用透视投影从近裁剪平面绘制一条射线/线到远裁剪平面。我有我认为是使用各种 OpenGL/图形编程指南中描述的方法通过单击鼠标生成的正确标准化的世界坐标。 我遇到的问题是我的光线似乎
我是一名优秀的程序员,十分优秀!