13 static PyObject *selftest(PyObject *self, PyObject *args)
15 if (!PyArg_ParseTuple(args, ""))
18 return Py_BuildValue("i", !bupsplit_selftest());
22 static PyObject *blobbits(PyObject *self, PyObject *args)
24 if (!PyArg_ParseTuple(args, ""))
26 return Py_BuildValue("i", BUP_BLOBBITS);
30 static PyObject *splitbuf(PyObject *self, PyObject *args)
32 unsigned char *buf = NULL;
33 int len = 0, out = 0, bits = -1;
35 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
37 out = bupsplit_find_ofs(buf, len, &bits);
38 return Py_BuildValue("ii", out, bits);
42 static PyObject *bitmatch(PyObject *self, PyObject *args)
44 unsigned char *buf1 = NULL, *buf2 = NULL;
45 int len1 = 0, len2 = 0;
48 if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
52 for (byte = 0; byte < len1 && byte < len2; byte++)
54 int b1 = buf1[byte], b2 = buf2[byte];
57 for (bit = 0; bit < 8; bit++)
58 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
64 return Py_BuildValue("i", byte*8 + bit);
68 static PyObject *firstword(PyObject *self, PyObject *args)
70 unsigned char *buf = NULL;
74 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
80 v = ntohl(*(uint32_t *)buf);
81 return PyLong_FromUnsignedLong(v);
91 static void to_bloom_address_bitmask4(const bits40_t *buf,
92 const int nbits, uint64_t *v, unsigned char *bitmask)
97 mask = (1<<nbits) - 1;
98 raw = (((uint64_t)ntohl(buf->high)) << 8) | buf->low;
99 bit = (raw >> (37-nbits)) & 0x7;
100 *v = (raw >> (40-nbits)) & mask;
104 static void to_bloom_address_bitmask5(const uint32_t *buf,
105 const int nbits, uint32_t *v, unsigned char *bitmask)
110 mask = (1<<nbits) - 1;
112 bit = (raw >> (29-nbits)) & 0x7;
113 *v = (raw >> (32-nbits)) & mask;
118 #define BLOOM_SET_BIT(name, address, itype, otype) \
119 static void name(unsigned char *bloom, const void *buf, const int nbits)\
121 unsigned char bitmask;\
123 address((itype *)buf, nbits, &v, &bitmask);\
124 bloom[16+v] |= bitmask;\
126 BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
127 BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
130 #define BLOOM_GET_BIT(name, address, itype, otype) \
131 static int name(const unsigned char *bloom, const void *buf, const int nbits)\
133 unsigned char bitmask;\
135 address((itype *)buf, nbits, &v, &bitmask);\
136 return bloom[16+v] & bitmask;\
138 BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
139 BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
142 static PyObject *bloom_add(PyObject *self, PyObject *args)
144 unsigned char *sha = NULL, *bloom = NULL;
146 int len = 0, blen = 0, nbits = 0, k = 0;
148 if (!PyArg_ParseTuple(args, "w#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
151 if (blen < 16+(1<<nbits) || len % 20 != 0)
158 for (end = sha + len; sha < end; sha += 20/k)
159 bloom_set_bit5(bloom, sha, nbits);
165 for (end = sha + len; sha < end; sha += 20/k)
166 bloom_set_bit4(bloom, sha, nbits);
172 return Py_BuildValue("i", len/20);
175 static PyObject *bloom_contains(PyObject *self, PyObject *args)
177 unsigned char *sha = NULL, *bloom = NULL;
178 int len = 0, blen = 0, nbits = 0, k = 0;
182 if (!PyArg_ParseTuple(args, "t#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
192 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
193 if (!bloom_get_bit5(bloom, sha, nbits))
194 return Py_BuildValue("Oi", Py_None, steps);
200 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
201 if (!bloom_get_bit4(bloom, sha, nbits))
202 return Py_BuildValue("Oi", Py_None, steps);
207 return Py_BuildValue("Oi", Py_True, k);
211 static uint32_t _extract_bits(unsigned char *buf, int nbits)
215 mask = (1<<nbits) - 1;
216 v = ntohl(*(uint32_t *)buf);
217 v = (v >> (32-nbits)) & mask;
220 static PyObject *extract_bits(PyObject *self, PyObject *args)
222 unsigned char *buf = NULL;
223 int len = 0, nbits = 0;
225 if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
231 return PyLong_FromUnsignedLong(_extract_bits(buf, nbits));
236 unsigned char bytes[20];
248 static int _cmp_sha(const struct sha *sha1, const struct sha *sha2)
251 for (i = 0; i < 20; i++)
252 if (sha1->bytes[i] != sha2->bytes[i])
253 return sha1->bytes[i] - sha2->bytes[i];
258 static void _fix_idx_order(struct idx **idxs, int *last_i)
261 int low, mid, high, c = 0;
264 if (idxs[*last_i]->cur >= idxs[*last_i]->end)
266 idxs[*last_i] = NULL;
279 mid = (low + high) / 2;
280 c = _cmp_sha(idx->cur, idxs[mid]->cur);
292 memmove(&idxs[mid+1], &idxs[mid], (*last_i-mid)*sizeof(struct idx *));
297 static uint32_t _get_idx_i(struct idx *idx)
299 if (idx->cur_name == NULL)
300 return idx->name_base;
301 return ntohl(*idx->cur_name) + idx->name_base;
305 static PyObject *merge_into(PyObject *self, PyObject *args)
307 PyObject *ilist = NULL;
308 unsigned char *fmap = NULL;
309 struct sha *sha_ptr, *last = NULL;
310 uint32_t *table_ptr, *name_ptr;
311 struct idx **idxs = NULL;
312 int flen = 0, bits = 0, i;
313 uint32_t total, count, prefix;
317 if (!PyArg_ParseTuple(args, "w#iIO", &fmap, &flen, &bits, &total, &ilist))
320 num_i = PyList_Size(ilist);
321 idxs = (struct idx **)PyMem_Malloc(num_i * sizeof(struct idx *));
323 for (i = 0; i < num_i; i++)
325 long len, sha_ofs, name_map_ofs;
326 idxs[i] = (struct idx *)PyMem_Malloc(sizeof(struct idx));
327 PyObject *itup = PyList_GetItem(ilist, i);
328 if (!PyArg_ParseTuple(itup, "t#llli", &idxs[i]->map, &idxs[i]->bytes,
329 &len, &sha_ofs, &name_map_ofs, &idxs[i]->name_base))
331 idxs[i]->cur = (struct sha *)&idxs[i]->map[sha_ofs];
332 idxs[i]->end = &idxs[i]->cur[len];
333 idxs[i]->cur_name = (uint32_t *)&idxs[i]->map[name_map_ofs];
335 table_ptr = (uint32_t *)&fmap[12];
336 sha_ptr = (struct sha *)&table_ptr[1<<bits];
337 name_ptr = (uint32_t *)&sha_ptr[total];
346 if (count % 102424 == 0 && istty)
347 fprintf(stderr, "midx: writing %.2f%% (%d/%d)\r",
348 count*100.0/total, count, total);
350 new_prefix = _extract_bits((unsigned char *)idx->cur, bits);
351 while (prefix < new_prefix)
352 table_ptr[prefix++] = htonl(count);
353 if (last == NULL || _cmp_sha(last, idx->cur) != 0)
355 memcpy(sha_ptr++, idx->cur, 20);
356 *name_ptr++ = htonl(_get_idx_i(idx));
360 if (idx->cur_name != NULL)
362 _fix_idx_order(idxs, &last_i);
365 table_ptr[prefix] = htonl(count);
368 return PyLong_FromUnsignedLong(count);
372 // I would have made this a lower-level function that just fills in a buffer
373 // with random values, and then written those values from python. But that's
374 // about 20% slower in my tests, and since we typically generate random
375 // numbers for benchmarking other parts of bup, any slowness in generating
376 // random bytes will make our benchmarks inaccurate. Plus nobody wants
377 // pseudorandom bytes much except for this anyway.
378 static PyObject *write_random(PyObject *self, PyObject *args)
380 uint32_t buf[1024/4];
381 int fd = -1, seed = 0, verbose = 0;
383 long long len = 0, kbytes = 0, written = 0;
385 if (!PyArg_ParseTuple(args, "iLii", &fd, &len, &seed, &verbose))
390 for (kbytes = 0; kbytes < len/1024; kbytes++)
393 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
395 ret = write(fd, buf, sizeof(buf));
399 if (ret < (int)sizeof(buf))
401 if (verbose && kbytes/1024 > 0 && !(kbytes%1024))
402 fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
405 // handle non-multiples of 1024
409 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
411 ret = write(fd, buf, len % 1024);
418 fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
419 return Py_BuildValue("L", written);
423 static PyObject *random_sha(PyObject *self, PyObject *args)
425 static int seeded = 0;
426 uint32_t shabuf[20/4];
431 assert(sizeof(shabuf) == 20);
436 if (!PyArg_ParseTuple(args, ""))
439 memset(shabuf, 0, sizeof(shabuf));
440 for (i=0; i < 20/4; i++)
441 shabuf[i] = random();
442 return Py_BuildValue("s#", shabuf, 20);
446 static PyObject *open_noatime(PyObject *self, PyObject *args)
448 char *filename = NULL;
449 int attrs, attrs_noatime, fd;
450 if (!PyArg_ParseTuple(args, "s", &filename))
457 attrs |= O_LARGEFILE;
459 attrs_noatime = attrs;
461 attrs_noatime |= O_NOATIME;
463 fd = open(filename, attrs_noatime);
464 if (fd < 0 && errno == EPERM)
466 // older Linux kernels would return EPERM if you used O_NOATIME
467 // and weren't the file's owner. This pointless restriction was
468 // relaxed eventually, but we have to handle it anyway.
469 // (VERY old kernels didn't recognized O_NOATIME, but they would
470 // just harmlessly ignore it, so this branch won't trigger)
471 fd = open(filename, attrs);
474 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
475 return Py_BuildValue("i", fd);
479 static PyObject *fadvise_done(PyObject *self, PyObject *args)
483 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
485 #ifdef POSIX_FADV_DONTNEED
486 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
488 return Py_BuildValue("");
492 static PyMethodDef faster_methods[] = {
493 { "selftest", selftest, METH_VARARGS,
494 "Check that the rolling checksum rolls correctly (for unit tests)." },
495 { "blobbits", blobbits, METH_VARARGS,
496 "Return the number of bits in the rolling checksum." },
497 { "splitbuf", splitbuf, METH_VARARGS,
498 "Split a list of strings based on a rolling checksum." },
499 { "bitmatch", bitmatch, METH_VARARGS,
500 "Count the number of matching prefix bits between two strings." },
501 { "firstword", firstword, METH_VARARGS,
502 "Return an int corresponding to the first 32 bits of buf." },
503 { "bloom_contains", bloom_contains, METH_VARARGS,
504 "Check if a bloom filter of 2^nbits bytes contains an object" },
505 { "bloom_add", bloom_add, METH_VARARGS,
506 "Add an object to a bloom filter of 2^nbits bytes" },
507 { "extract_bits", extract_bits, METH_VARARGS,
508 "Take the first 'nbits' bits from 'buf' and return them as an int." },
509 { "merge_into", merge_into, METH_VARARGS,
510 "Merges a bunch of idx and midx files into a single midx." },
511 { "write_random", write_random, METH_VARARGS,
512 "Write random bytes to the given file descriptor" },
513 { "random_sha", random_sha, METH_VARARGS,
514 "Return a random 20-byte string" },
515 { "open_noatime", open_noatime, METH_VARARGS,
516 "open() the given filename for read with O_NOATIME if possible" },
517 { "fadvise_done", fadvise_done, METH_VARARGS,
518 "Inform the kernel that we're finished with earlier parts of a file" },
519 { NULL, NULL, 0, NULL }, // sentinel
522 PyMODINIT_FUNC init_helpers(void)
524 Py_InitModule("_helpers", faster_methods);
525 istty = isatty(2) || getenv("BUP_FORCE_TTY");