[PATCH v3 4/4] virtio_test: pregenerate random numbers

From: Guo Zhi
Date: Fri Jul 08 2022 - 22:28:55 EST


random numbers on data path is expensive, using a rand pool to
pregenerate them before tests start

Signed-off-by: Guo Zhi <qtxuning1999@xxxxxxxxxxx>
---
tools/virtio/virtio_test.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/tools/virtio/virtio_test.c b/tools/virtio/virtio_test.c
index 8f3ef3a78..7cc43e5bf 100644
--- a/tools/virtio/virtio_test.c
+++ b/tools/virtio/virtio_test.c
@@ -23,6 +23,7 @@
#define MAX_SG_FRAGS 8UL
#define RINGSIZE 256
#define INDIRECT_TABLE_SIZE (RINGSIZE * sizeof(struct vring_desc) * 8)
+#define RAND_POOL_SIZE 4096

void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;

@@ -199,6 +200,8 @@ static void run_test(struct vdev_info *dev, struct vq_info *vq,
{
struct scatterlist sg[MAX_SG_FRAGS];
int sg_size = 0;
+ int rand_pool[RAND_POOL_SIZE];
+ unsigned int rand_idx;
long started = 0, completed = 0, next_reset = reset_n;
long completed_before, started_before;
int r, test = 1;
@@ -213,6 +216,10 @@ static void run_test(struct vdev_info *dev, struct vq_info *vq,
next_reset = INT_MAX;
}

+ /* Init rand pool */
+ for (rand_idx = 0; rand_idx < RAND_POOL_SIZE; ++rand_idx)
+ rand_pool[rand_idx] = random();
+
/* Don't kfree indirect_table. */
__kfree_ignore_start = dev->indirect_table;
__kfree_ignore_end = dev->indirect_table + dev->indirect_table_size;
@@ -225,11 +232,13 @@ static void run_test(struct vdev_info *dev, struct vq_info *vq,
do {
const bool reset = completed > next_reset;
if (random_batch)
- batch = (random() % vq->vring.num) + 1;
+ batch = (rand_pool[rand_idx++ % RAND_POOL_SIZE]
+ % vq->vring.num) + 1;

while (started < bufs &&
(started - completed) < batch) {
- sg_size = random() % (MAX_SG_FRAGS - 1) + 1;
+ sg_size = (rand_pool[rand_idx++ % RAND_POOL_SIZE]
+ % (MAX_SG_FRAGS - 1)) + 1;
sg_init_table(sg, sg_size);
for (int i = 0; i < sg_size; ++i)
sg_set_buf(&sg[i], dev->buf + i, 0x1);
--
2.17.1