File: data_generator.h

package info (click to toggle)
lizardfs 3.12.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,064 kB
  • sloc: cpp: 91,899; sh: 9,341; python: 3,878; ansic: 3,109; pascal: 128; makefile: 57
file content (213 lines) | stat: -rw-r--r-- 7,369 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
   Copyright 2013-2015 Skytechnology sp. z o.o.

   This file is part of LizardFS.

   LizardFS is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, version 3.

   LizardFS is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with LizardFS. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#include "config.h"

#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <vector>

#include "common/portable_endian.h"

#include "utils/asserts.h"
#include "utils/configuration.h"

/*
 * This class generates files in the following format:
 * - first 8 bytes: size of the file (thus the minimal size is 8 bytes)
 * - then a sequence of 8-byte blocks, each block contains a value
 *     (offset + 0x0807060504030201) % 2^64
 * If the file size does not divide by 8 the last block is truncated.
 * All numbers (uint64) are in big endian format
 * (it is easier for a human to read the hexdump -C of such file)
 */
class DataGenerator {
public:
	static void createFile(const std::string& name, uint64_t size) {
		int fd = open(name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, (mode_t)0644);
		utils_passert(fd >= 0);
		fillFileWithProperData(fd, size);
		utils_zassert(close(fd));
	}

	static void overwriteFile(const std::string& name) {
		struct stat fileInformation;
		utils_zassert(stat(name.c_str(), &fileInformation));
		int fd = open(name.c_str(), O_WRONLY, (mode_t)0644);
		utils_passert(fd >= 0);
		fillFileWithProperData(fd, fileInformation.st_size);
		utils_zassert(close(fd));
	}

	static void validateFile(int fd, const std::string &name) {
		std::string error;

		/* Check the file size */
		struct stat fileInformation;
		utils_zassert(stat(name.c_str(), &fileInformation));
		uint64_t actualSize = fileInformation.st_size;
		uint64_t expectedSize;

		if(read(fd, &expectedSize, sizeof(expectedSize)) != sizeof(expectedSize)) {
			// The file if too short, so the first bytes are corrupted.
			throw std::length_error("(inode " + std::to_string(fileInformation.st_ino) + ")"
					" file too short (" + std::to_string(actualSize) + " bytes)");
		}
		expectedSize = be64toh(expectedSize);
		if (expectedSize != (uint64_t)fileInformation.st_size) {
			error = "(inode " + std::to_string(fileInformation.st_ino) + ")"
					" file should be " + std::to_string(expectedSize) +
					" bytes long, but is " + std::to_string(actualSize) + " bytes long\n";
		}

		/* Check the data */
		off_t currentOffset = sizeof(actualSize);
		uint64_t size = actualSize - sizeof(actualSize);
		std::vector<char> actualBuffer(UtilsConfiguration::blockSize());
		std::vector<char> properBuffer(UtilsConfiguration::blockSize());
		while (size > 0) {
			uint64_t bytesToRead = size;
			if (bytesToRead > properBuffer.size()) {
				bytesToRead = properBuffer.size();
			}
			properBuffer.resize(bytesToRead);
			fillBufferWithProperData(properBuffer, currentOffset);
			utils_passert(read(fd, actualBuffer.data(), bytesToRead) == (ssize_t)bytesToRead);
			size -= bytesToRead;
			// memcmp is very fast, use it to check if everything is OK
			if (memcmp(actualBuffer.data(), properBuffer.data(), bytesToRead) == 0) {
				currentOffset += bytesToRead;
				continue;
			}
			// if not -- find the byte which is corrupted
			for (size_t i = 0; i < bytesToRead; ++i) {
				if (actualBuffer[i] != properBuffer[i]) {
					std::stringstream ss;
					ss << "(inode " << fileInformation.st_ino << ")"
							<< " data mismatch at offset " << i << ". Expected/actual:\n";
					for (size_t j = i; j < bytesToRead && j < i + 32; ++j) {
						ss << std::hex << std::setfill('0') << std::setw(2)
								<< static_cast<int>(static_cast<unsigned char>(properBuffer[j]))
								<< " ";
					}
					ss << "\n";
					for (size_t j = i; j < bytesToRead && j < i + 32; ++j) {
						ss << std::hex << std::setfill('0') << std::setw(2)
								<< static_cast<int>(static_cast<unsigned char>(actualBuffer[j]))
								<< " ";
					}
					throw std::invalid_argument(error + ss.str());
				}
			}
			utils_mabort("memcmp returned non-zero, but there is no difference");
		}
		if (!error.empty()) {
			throw std::length_error(error + "The rest of the file is OK");
		}
	}

	/*
	 * This function checks if the file contains proper data
	 * generated by DataStream::createFile and throws an std::exception
	 * in case of the data is corrupted. If 'repeat_after' is set, validation
	 * should be repeated after given time (for testing various caches).
	 */
	static void validateFile(const std::string& name, size_t repeat_after_ms) {
		int fd = open(name.c_str(), O_RDONLY);
		utils_passert(fd != -1);
		if (repeat_after_ms > 0) {
			try {
				validateFile(fd, name);
			} catch (...) {
			}
			usleep(1000 * repeat_after_ms);
			lseek(fd, 0, SEEK_SET);
			validateFile(fd, name);
		} else {
			validateFile(fd, name);
		}
		utils_zassert(close(fd));
	}


protected:
	static void fillBufferWithProperData(std::vector<char>& buffer, off_t offset) {
		size_t size = buffer.size();
		if (offset % 8 == 0 && size % 8 == 0) {
			fillAlignedBufferWithProperData(buffer, offset);
			return;
		}
		/*
		 * If the buffer or offset is not aligned
		 * we will create aligned buffer big enough to
		 * be a superset of the buf, fill it with data
		 * and copy the proper part of it
		 */
		off_t alignedOffset = (offset / 8) * 8;
		std::vector<char> alignedBuffer(16 + (size / 8) * 8);
		fillAlignedBufferWithProperData(alignedBuffer, alignedOffset);
		memcpy(buffer.data(), alignedBuffer.data() + (offset - alignedOffset), size);
	}

	/**
	 * This function requires both offset and size to be multiples of 8
	 */
	static void fillAlignedBufferWithProperData(std::vector<char>& buffer, off_t offset) {
		typedef uint64_t BlockType;
		utils_massert(sizeof(BlockType) == 8);
		utils_massert(offset % sizeof(BlockType) == 0);
		utils_massert(buffer.size() % sizeof(BlockType) == 0);
		BlockType *blocks = (BlockType*)buffer.data();
		for (size_t i = 0; i < buffer.size() / sizeof(BlockType); ++i) {
			BlockType block = htobe64(0x0807060504030201ULL + offset);
			blocks[i] = block;
			offset += sizeof(BlockType);
		}
	}

	static void fillFileWithProperData(int fd, uint64_t size) {
		utils_massert(fd >= 0);

		/* Write the size of the file */
		uint64_t serializedSize = htobe64(size);
		utils_massert(size >= sizeof(serializedSize));
		utils_passert(write(fd, &serializedSize, sizeof(serializedSize))== sizeof(serializedSize));

		/* Write the data */
		size -= sizeof(serializedSize);
		off_t currentOffset = sizeof(serializedSize);
		std::vector<char> buffer(UtilsConfiguration::blockSize());
		while (size > 0) {
			size_t bytesToWrite = size;
			if (bytesToWrite > buffer.size()) {
				bytesToWrite = buffer.size();
			}
			buffer.resize(bytesToWrite);
			fillBufferWithProperData(buffer, currentOffset);
			utils_passert(write(fd, buffer.data(), bytesToWrite) == (ssize_t)bytesToWrite);
			size -= bytesToWrite;
			currentOffset += bytesToWrite;
		}
	}
};