【发布时间】:2013-12-01 02:31:07
【问题描述】:
我正在为我的一个课程编写一个文件系统。这个功能大大降低了我的表现,我不知道为什么。我一直盯着这段代码太久了,我可能遗漏了一些非常明显的东西。有人知道为什么这个功能会这么慢吗?
int getFreeDataBlock(struct disk *d, unsigned int dataBlockNumber)
{
if (d == NULL)
{
fprintf(stderr, "Invalid disk pointer to getFreeDataBlock()\n");
errorCheck();
return -1;
}
// Allocate a buffer
char *buffer = (char *) malloc(d->blockSize * sizeof(char));
if (buffer == NULL)
{
fprintf(stderr, "Out of memory.\n");
errorCheck();
return -1;
}
do {
// Read a block from the disk
diskread(d, buffer, dataBlockNumber);
// Cast to appropriate struct
struct listDataBlock *block = (struct listDataBlock *) buffer;
unsigned int i;
for (i = 0; i < DATABLOCK_FREE_SLOT_LENGTH; ++i)
{
// We are in the last datalisting block...and out of slots...break
if (block->listOfFreeBlocks[i] == -2)
{
break;
}
if (block->listOfFreeBlocks[i] != -1)
{
int returnValue = block->listOfFreeBlocks[i];
// MARK THIS AS USED NOW
block->listOfFreeBlocks[i] = -1;
diskwriteNoSync(d, buffer, dataBlockNumber);
// No memory leaks
free(buffer);
return returnValue;
}
}
// Ok, nothing in this data block, move to next
dataBlockNumber = block->nextDataBlock;
} while (dataBlockNumber != -1);
// Nope, didn't find any...disk must be full
free(buffer);
fprintf(stderr, "DISK IS FULL\n");
errorCheck();
return -1;
}
正如您从 gprof 中看到的那样,diskread() 和 diskwriteNoSync() 都需要大量时间?
% cumulative self self total
time seconds seconds calls ms/call ms/call name
99.45 12.25 12.25 2051 5.97 5.99 getFreeDataBlock
0.24 12.28 0.03 2220903 0.00 0.00 diskread
0.24 12.31 0.03 threadFunc
0.08 12.32 0.01 2048 0.00 6.00 writeHelper
0.00 12.32 0.00 6154 0.00 0.00 diskwriteNoSync
0.00 12.32 0.00 2053 0.00 0.00 validatePath
还是我没有正确理解输出?
感谢您的帮助。
【问题讨论】:
-
好吧,你有一个
do循环(运行次数未知),其中包含一个for循环,该循环最多运行DATABLOCK_FREE_SLOT_LENGTH次(DATABLOCK_FREE_SLOT_LENGTH的值是多少? ) 内部循环 I/O 内部正在发生(diskread和diskwriteNoSync)。相对于 CPU 时间,I/O 需要大量时间。这就是我要看的地方。 -
是的,在喝了一杯 espresso 之后,我意识到它实际上只是在 FOR 循环中为大量磁盘块执行 1023 次所需的时间……所以 I/O 本身就没有了t 的问题,但我在做什么。重新编写了我的磁盘块实现,它现在正在运行。
标签: c filesystems profiling gprof