Skip to content

Commit

Permalink
lkl: Add test that reads /proc/cpuinfo
Browse files Browse the repository at this point in the history
This test reads from the file from two descriptors at the same time and
verifies that the file isn't empty and the contents read from both
descriptors are the same.

The current information in cpuinfo is hardcoded.
  • Loading branch information
AntonioND committed Sep 10, 2020
1 parent 559f8ae commit eced6cd
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/lkl/Targets
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ LDLIBS_cptofs-y := -larchive
LDLIBS_cptofs-$(LKL_HOST_CONFIG_NEEDS_LARGP) += -largp

progs-y += tests/boot
progs-y += tests/cpuinfo
progs-y += tests/disk
progs-y += tests/net-test

38 changes: 38 additions & 0 deletions tools/lkl/lib/posix-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
Expand Down Expand Up @@ -311,6 +312,42 @@ static long _gettid(void)
#endif
}

static unsigned int cpuinfo_get(char *buffer, unsigned int buffer_len)
{
/* TODO: The output of this function is hardcoded for now. */
int len;

len = snprintf(buffer, buffer_len,
"processor : 0\n"
"cpu family : 6\n"
"model : 158\n"
"model name : Intel(R) Xeon(R) CPU E3-1280 v6 @ 3.90GHz\n"
"stepping : 9\n"
"microcode : 0xb4\n"
"cpu MHz : 800.063\n"
"cache size : 8192 KB\n"
"physical id : 0\n"
"siblings : 1\n"
"core id : 0\n"
"cpu cores : 1\n"
"apicid : 0\n"
"initial apicid : 0\n"
"fpu : yes\n"
"fpu_exception : yes\n"
"cpuid level : 22\n"
"wp : yes\n"
"flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse3 clflush dts ac pi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bt rep_good nopl x topology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm aabm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d\n"
"bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs\n"
"bogomips : 7824.00\n"
"clflush size : 64\n"
"cache_alignment : 64\n"
"address sizes : 39 bits physical, 48 bits virtual\n"
"power management: \n"
"\n");

return len;
}

struct lkl_host_operations lkl_host_ops = {
.panic = panic,
.thread_create = thread_create,
Expand Down Expand Up @@ -344,6 +381,7 @@ struct lkl_host_operations lkl_host_ops = {
.gettid = _gettid,
.jmp_buf_set = jmp_buf_set,
.jmp_buf_longjmp = jmp_buf_longjmp,
.cpuinfo_get = cpuinfo_get,
};

static int fd_get_capacity(struct lkl_disk disk, unsigned long long *res)
Expand Down
1 change: 1 addition & 0 deletions tools/lkl/tests/Build
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
boot-y += boot.o test.o
cpuinfo-y += cpuinfo.o test.o
disk-y += disk.o cla.o test.o
net-test-y += net-test.o cla.o test.o
116 changes: 116 additions & 0 deletions tools/lkl/tests/cpuinfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <lkl.h>
#include <lkl_host.h>

#include "test.h"

LKL_TEST_CALL(start_kernel, lkl_start_kernel, 0, &lkl_host_ops,
"mem=16M loglevel=8");

static int lkl_test_cpuinfo(void)
{
/* The buffer size must be a multiple of the read sizes */
#define BUF_SIZE (64 * 1024)
#define READ_SIZE_1 16
#define READ_SIZE_2 32

int ret;
int err;
int f1, f2;
static char buf1[BUF_SIZE], buf2[BUF_SIZE];
long read1, read2;
int done1, done2;
long totalread1, totalread2;

ret = TEST_FAILURE;

err = lkl_mount_fs("proc");
if (err < 0) {
lkl_test_logf("failed to mount /proc\n");
return TEST_FAILURE;
}

f1 = lkl_sys_open("/proc/cpuinfo", LKL_O_RDONLY, 0);
if (f1 < 0) {
lkl_test_logf("failed to open /proc/cpuinfo (f1): %s\n",
lkl_strerror(f1));
return TEST_FAILURE;
}

f2 = lkl_sys_open("/proc/cpuinfo", LKL_O_RDONLY, 0);
if (f2 < 0) {
lkl_sys_close(f1);
lkl_test_logf("failed to open /proc/cpuinfo (f2): %s\n",
lkl_strerror(f2));
return TEST_FAILURE;
}

totalread1 = 0;
totalread2 = 0;

done1 = 0;
done2 = 0;

while (!done1 || !done2) {
if (totalread1 >= (BUF_SIZE - READ_SIZE_1)) {
lkl_test_logf("file is too big\n");
totalread1 = 0;
break;
}

if (totalread2 >= (BUF_SIZE - READ_SIZE_2)) {
lkl_test_logf("file is too big\n");
totalread2 = 0;
break;
}

if (!done1) {
read1 = lkl_sys_read(f1, &(buf1[totalread1]),
READ_SIZE_1);
if (read1 <= 0)
done1 = 1;
totalread1 += read1;
}

if (!done2) {
read2 = lkl_sys_read(f2, &(buf2[totalread2]),
READ_SIZE_2);
if (read2 <= 0)
done2 = 1;
totalread2 += read2;
}
}

lkl_sys_close(f1);
lkl_sys_close(f2);

if (totalread1 == 0) {
lkl_test_logf("file is empty\n");
} else if (totalread1 != totalread2) {
lkl_test_logf("sizes don't match: %lu != %lu\n",
totalread1, totalread2);
} else {
if (memcmp(buf1, buf2, totalread1) == 0)
ret = TEST_SUCCESS;
else
lkl_test_logf("read contents don't match");
}

return ret;
}

struct lkl_test tests[] = {
LKL_TEST(start_kernel),
LKL_TEST(cpuinfo),
};

int main(int argc, const char **argv)
{
lkl_host_ops.print = lkl_test_log;

return lkl_test_run(tests, sizeof(tests)/sizeof(struct lkl_test),
"cpuinfo");
}
8 changes: 8 additions & 0 deletions tools/lkl/tests/cpuinfo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

script_dir=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd)
source $script_dir/test.sh

lkl_test_plan 1 "cpuinfo"
lkl_test_run 1
lkl_test_exec $script_dir/cpuinfo
1 change: 1 addition & 0 deletions tools/lkl/tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def end(self, obj):

tests = [
'boot.sh',
'cpuinfo.sh',
'disk.sh -t ext4',
'disk.sh -t btrfs',
'disk.sh -t vfat',
Expand Down

0 comments on commit eced6cd

Please sign in to comment.