Skip to content

Commit

Permalink
Add sort_test.c for Intel PT case
Browse files Browse the repository at this point in the history
Signed-off-by: qwang59 <[email protected]>
  • Loading branch information
qwang59 committed Apr 1, 2024
1 parent b07d44a commit a329762
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

CC ?= "${CC}" -g -Wall
CFLAGS += -DMAINLINE -I./
BIN = cpl branch psb nonroot_test negative_test
BIN = cpl branch psb nonroot_test negative_test sort_test
LFLAGS = -L./ -lipt


Expand All @@ -25,5 +25,8 @@ nonroot_test:
negative_test:
$(CC) -o $@ $@.c utils.c ${CFLAGS} ${LFLAGS}

sort_test:
$(CC) -o $@ $@.c utils.c ${CFLAGS} ${LFLAGS}

clean:
rm -rf $(BIN) *.o
73 changes: 73 additions & 0 deletions pt/sort_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2024 Intel Corporation.
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#define ARRAY_LEN 3000

size_t partition(int *a, size_t low, size_t high)
{
int pivot = a[high];
size_t i = low - 1;

for (size_t j = low; j <= high - 1; j++) {
if (a[j] < pivot) {
i++;
swap(&a[i], &a[j]);
}
}
swap(&a[i + 1], &a[high]);
return (i + 1);
}

static inline void start(struct timeval *tm)
{
gettimeofday(tm, NULL);
}

void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

static inline void stop(struct timeval *tm1, struct timeval *tm2)
{
unsigned long long t = 1000 * (tm2->tv_sec - tm1->tv_sec) +
(tm2->tv_usec - tm1->tv_usec) / 1000;
printf("%llu ms\n", t);
}

void quick_sort(int *a, size_t low, size_t high)
{
if (low < high) {
size_t pivot = partition(a, low, high);

quick_sort(a, low, pivot - 1);
quick_sort(a, pivot + 1, high);
}
}

void sort_array(void)
{
printf("Quick sorting array of %zu elements\n", ARRAY_LEN);
int data[ARRAY_LEN];

for (size_t i = 0; i < ARRAY_LEN; ++i)
data[i] = rand();

quick_sort(data, 0, ARRAY_LEN - 1);
}

int main(void)
{
struct timeval tm1, tm2;

start(&tm1);
sort_array();
gettimeofday(&tm2, NULL);
stop(&tm1, &tm2);
return 0;
}

0 comments on commit a329762

Please sign in to comment.