Exemplo n.º 1
0
int
main()
{
	struct rbTree* t = treeInit();
   
	struct rbTree *root = t = rbTreeInsert(t , 3);
	rbTreeInsert(t , 8);
	struct rbTree * x = rbTreeInsert(t , 4);
	struct rbTree * y = rbTreeInsert(t , 14);
	rbTreeInsert(t , 9);
	rbTreeInsert(t , 2);
	rbTreeInsert(t , 0);
	treeWalk(t);

//	treeWalk(t);
//	treeDelete(x);
//	treeWalk(root);
//	treeDelete(y);
//	treeWalk(root);

//	printf("\nmax = %d min = %d \n", treeMax(t) -> k, treeMin(t) -> k);
	
//	if(treeSearch(t, 3) != NULL)
//		printf("found\n");
	
//	printf("%d\n",t  -> k); 
//	t = treeSuccessor(t);
//	printf("%d\n",t  -> k); 
//	t = treePredecessor(t);
//	printf("%d\n",t  -> k); 
}
Exemplo n.º 2
0
static int __init kread_init(void) 
{ 	
	unsigned int j = 0;
	unsigned int i = 0;
	unsigned int state = 0; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	unsigned int choiceSeq = 0;
	long long int timeStart = 0;
	long long int timeEnd = 0;
	size_t n;

	loff_t offset = 0; // For write.
	loff_t file_offset = 0; // For read.
	mm_segment_t fs;
	long move; // Step at reading.
	int endRWC = 0;
	
	// Initialization of rbTree.
	struct dataRBTree *itemRBTree;
	struct rb_root rbTree = RB_ROOT;
	//struct rb_node *node;
	
	// Initialization of hash table.
	/*int size = 0;
	struct list_head *hashList;
	struct dataListHash *itemHashTable;
	size = numberBuckets * sizeof(*hashList);
	hashList = kmalloc(size, GFP_KERNEL);
	for (i = 0; i < numberBuckets; ++i)
		INIT_LIST_HEAD(&hashList[i]);*/

	fs = get_fs(); 
	set_fs(get_ds()); 
	
	if (file != NULL) // If set module parameter "file".
		strcpy(pathToInputFile, file);

	fileInput = filp_open(pathToInputFile, O_RDONLY, 0); // Open the file for read.
	
	if (isOpenIncorrect(fileInput, pathToInputFile, fs))
		return -ENOENT;

	if (log != NULL) // If set module parameter "log".
		strcpy(pathToOutputFile, log); 

   	fileOutput = filp_open(pathToOutputFile, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); // Open the file for write.
	
	if (isOpenIncorrect(fileOutput, pathToOutputFile, fs))
		return -ENOENT;
	
	move = vfs_llseek(fileInput, numberOfBytes, 0); // 0 means SEEK_SET. (set in begin of file)
	i = 0;
	//timeStart = ktime_to_ns(ktime_get());
	while (1) 
	{
		n = vfs_read(fileInput, buff, move, &file_offset);
		if (n == 0) // If the file is ended.
		{
			if (strlen(str) > 0) 
			{
				vfs_write(fileOutput, "New line: ", 10, &offset);
				vfs_write(fileOutput, str, strlen(str), &offset);
			}
			break;
		}

		for (j = 0; j < n; j++) 
		{
			if (buff[j] == ' ' || buff[j] == ':')
				continue;
			else if (buff[j] == 'q')
				choiceSeq = 1;
			else if (buff[j] == 'l')
				state = 1;
			else if ((buff[j] == 'b' || buff[j] == 'e') && state == 1)
				state = 2;
			else if ((buff[j] == 'a' || buff[j] == 'n') && state == 2)
				state = 3;
			else if ((state == 3 || state == 4) && buff[j] >= '0' && buff[j] <= '9') // Reads the number.
			{
				state = 4;
				str[i] = buff[j];
				str[i + 1] = '\0';
				i++;
			}
			else if (state == 4 && buff[j] == ',') // Read lba.
			{
				strcpy(strLba, str);
				i = 0;
				strcpy(str, ""); // Clean the string.
			}
			else if (state == 4 && !(buff[j] >= '0' && buff[j] <= '9')) // Read lba and len.
			{
				// Work with RBTree.
				itemRBTree = kmalloc(sizeof(*itemRBTree), GFP_KERNEL);
				kstrtoll(strLba, 10, &itemRBTree->lbaMain);
				itemRBTree->lbaAux = endRWC;
				kstrtoll(str, 10, &itemRBTree->length);
				endRWC += itemRBTree->length;
				//timeStart = ktime_to_ns(ktime_get());
				if (choiceSeq)
				{
					timeStart = ktime_to_ns(ktime_get());
					rbTreeCorrect(&rbTree, NULL, itemRBTree->lbaMain, itemRBTree->lbaAux, itemRBTree->length);
					timeEnd = ktime_to_ns(ktime_get());
					tostring(str, (timeEnd - timeStart));
					vfs_write(fileOutput, str, strlen(str), &offset);
					vfs_write(fileOutput, "\n", 1, &offset);
					//printk("Time: %lld\n", (timeEnd - timeStart));
				}
				else
					rbTreeInsert(&rbTree, itemRBTree);
				//timeEnd = ktime_to_ns(ktime_get());
				//tostring(str, (timeEnd - timeStart));
				//vfs_write(fileOutput, str, strlen(str), &offset);
				//vfs_write(fileOutput, "\n", 1, &offset);
				//printk("Time: %lld\n", (timeEnd - timeStart));
				
				//if (countOfNodesRBTree > 1000)
				//{
					//timeStart = ktime_to_ns(ktime_get());
					//removeDataFromRBTree(&rbTree, 1000);
					//timeEnd = ktime_to_ns(ktime_get());
					//tostring(str, (timeEnd - timeStart));
					//vfs_write(fileOutput, str, strlen(str), &offset);
					//vfs_write(fileOutput, "\n", 1, &offset);
					//printk("Time: %lld\n", (timeEnd - timeStart));
				//}
				
				
				// Work with hash table.
				/*itemHashTable = kmalloc(sizeof(*itemHashTable), GFP_KERNEL);
				kstrtoll(strLba, 10, &itemHashTable->lbaMain);
				itemHashTable->lbaAux = endRWC;
				kstrtoll(str, 10, &itemHashTable->length);
				endRWC += itemHashTable->length;
				timeStart = ktime_to_ns(ktime_get());
				* if (choiceSeq)
				{
					timeStart = ktime_to_ns(ktime_get());
					hashTableCorrect(hashList, NULL, itemRBTree->lbaMain, itemRBTree->lbaAux, itemRBTree->length);
					timeEnd = ktime_to_ns(ktime_get());
					tostring(str, (timeEnd - timeStart));
					vfs_write(fileOutput, str, strlen(str), &offset);
					vfs_write(fileOutput, "\n", 1, &offset);
					//printk("Time: %lld\n", (timeEnd - timeStart));
				}
				else
					hashTableInsert(hashList, itemHashTable, choiceSeq);
				timeEnd = ktime_to_ns(ktime_get());
				tostring(str, (timeEnd - timeStart));
				vfs_write(fileOutput, str, strlen(str), &offset);
				vfs_write(fileOutput, "\n", 1, &offset);
				printk("Time: %lld\n", (timeEnd - timeStart));
				
				if (countOfNodesHashTable > 1000)
				{
					//timeStart = ktime_to_ns(ktime_get());
					removeDataFromHashTable(hashList, 1000);
					//timeEnd = ktime_to_ns(ktime_get());
					//tostring(str, (timeEnd - timeStart));
					//vfs_write(fileOutput, str, strlen(str), &offset);
					//vfs_write(fileOutput, "\n", 1, &offset);
					//printk("Time: %lld\n", (timeEnd - timeStart));
				}*/
				
				i = 0;
				strcpy(str, ""); // Clean the string.
				state = 0; // Go to search 'l'.
				choiceSeq = 0;
			}
		}
	}
	//timeEnd = ktime_to_ns(ktime_get());
	//tostring(str, (timeEnd - timeStart));
	//vfs_write(fileOutput, str, strlen(str), &offset);
	//vfs_write(fileOutput, "\n", 1, &offset);
	//printk("Time: %lld\n", (timeEnd - timeStart));
	set_fs(fs);

	filp_close(fileInput, NULL); // Close the input file.
	filp_close(fileOutput, NULL); // Close the output file.
	
	//removeDataFromRBTree(&rbTree, 100);
	//removeDataFromHashTable(hashList);
	
	//printk("Hash Table:\n");
	//hashTablePrint(hashList);
	/*printk("Root\n");
	for (node = rb_first(&rbTree); node; node = rb_next(node))
		printk("lbaMain=%lld lbaAux=%lld length=%lld\n", 
			rb_entry(node, struct dataRBTree, node)->lbaMain, 
			rb_entry(node, struct dataRBTree, node)->lbaAux, 
			rb_entry(node, struct dataRBTree, node)->length);*/
	
	return 0; 
}