Skip to content

Commit

Permalink
refactor(plugin): Cleanup and consolidate functions
Browse files Browse the repository at this point in the history
Cleanup, rename and consolidate plugin/op-related functions:
- Move and cleanup op-related declarations/macros from `plugin.h` to
  `op.h`
- Move plugin-related functions from `vaccel.c` to `plugin.c`
- Rename plugin-related functions and add `plugin_` prefix
- Cleanup and expose both `vaccel_plugin_load()` and
  `vaccel_parse_and_load()`
- Remove fs check from `vaccel_plugin_load()` so the user can provide
  libraries without a full path. `dlopen` will search in the library
  path if only a library filename is provided
- Remove redundant `misc.c|h` and related tests
- Remove `plugin_register|unregister()` from public headers, since they
  are not currently meant to be used explicitly

Signed-off-by: Kostis Papazafeiropoulos <[email protected]>
  • Loading branch information
papazof committed Jan 13, 2025
1 parent 3cfa48b commit 1a80696
Show file tree
Hide file tree
Showing 54 changed files with 431 additions and 585 deletions.
93 changes: 67 additions & 26 deletions docs/tests/mock_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,70 @@ can be used to emulate functionality when the actual implementation cannot be
controlled (ie. calls to external libraries). Function mocking is useful in unit
testing for creating isolated, self-contained tests.

For basic mock functions we use ```fff.h``` (fake function framework). An
example implementation can be found at
```test/core/test_misc_mock.cpp```:
For basic mock functions we use ```fff.h``` (fake function framework). A mock
implementation and its' use can be found in ```test/mock_virtio.cpp``` and
```test/core/test_session.cpp``` respectively.

A deducted example is provided below:
```cpp
// test/mock_virtio.cpp

#include "vaccel.h"

static struct vaccel_plugin plugin;
static struct vaccel_plugin_info plugin_info;

...

auto mock_virtio_plugin_virtio() -> struct vaccel_plugin *
{
plugin_info.name = "fake_virtio";
...
// set rest of the struct members
...
plugin.info = &plugin_info;

return &plugin;
}
```
```cpp
// test/core/test_session.cpp
#include "vaccel.h"
#include <catch.hpp>
#include <fff.h>
#include <utils.hpp>
...
DEFINE_FFF_GLOBALS;
#include <mock_virtio.hpp>
#include <vaccel.h>
DEFINE_FFF_GLOBALS;
extern "C" {
FAKE_VALUE_FUNC(int, get_available_plugins, enum vaccel_op_type);
FAKE_VALUE_FUNC(struct vaccel_plugin *, plugin_virtio);
}
...
TEST_CASE("get_plugins_mock", "[core_misc]")
TEST_CASE("session_virtio", "[core][session]")
{
struct vaccel_session session;
session.session_id = 123;

SECTION("return correct implementation")
{
get_available_plugins_fake.return_val = 15;
int result = vaccel_get_plugins(&session, VACCEL_NO_OP);
REQUIRE(result == 15);
}
int ret;
...
RESET_FAKE(plugin_virtio);
...
plugin_virtio_fake.custom_fake = mock_virtio_plugin_virtio;
...
// call functions using plugin_virtio()
...
REQUIRE(plugin_virtio_fake.call_count == <nr of function calls>);
}
```

Let us walk this through step by step.

We first include and initialize fff:
We first create a mock function that does the necessary argument modifications
and returns the expected result - here `mock_virtio_plugin_virtio()`.

Then, to actually use our function we need to include and initialize fff:
```cpp
...
#include <fff.h>
Expand All @@ -49,16 +78,28 @@ DEFINE_FFF_GLOBALS;

To declare a mock function, we use
```FAKE_VALUE_FUNC(<return type>, <name of function>, <arguments>)```.
In this example we want to alter the functionality of
```get_available_plugins()```:
In this example we want to alter the functionality of ```plugin_virtio()()```:
```cpp
FAKE_VALUE_FUNC(struct vaccel_plugin *, plugin_virtio);
```
After declaring the function, we need to reset its' state for each
```TEST_CASE```:
```cpp
RESET_FAKE(plugin_virtio);
```

and set the respective variable so it will substitute the actual function:
```cpp
FAKE_VALUE_FUNC(int, get_available_plugins, enum vaccel_op_type);
plugin_virtio_fake.custom_fake = mock_virtio_plugin_virtio;
```

Then inside ```TEST_CASE``` we can control function arguments/return:
After doing any calls using the mock function, we can verify its' use:
```cpp
get_available_plugins_fake.return_val = 15;
REQUIRE(plugin_virtio_fake.call_count == <nr of function calls>);
```
Now when ```get_available_plugins()``` is called in our test, it will return
```15```, which is self explanatory here.
Although this example implements a new function to mock the actual one, it is
possible to only define the function return value instead by using
```mock_virtio_plugin_virtio.return_val = <mock value>``` and skipping the
implementation of a mock ```plugin_virtio()``` entirely.
4 changes: 2 additions & 2 deletions examples/classify_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ int main(int argc, char *argv[])
if (ret)
goto close_session;

enum vaccel_op_type op_type = VACCEL_IMG_CLASS;
struct vaccel_arg read[2] = { { .size = sizeof(enum vaccel_op_type),
vaccel_op_t op_type = VACCEL_IMG_CLASS;
struct vaccel_arg read[2] = { { .size = sizeof(vaccel_op_t),
.buf = &op_type },
{ .size = image_size, .buf = image } };
struct vaccel_arg write[2] = {
Expand Down
4 changes: 2 additions & 2 deletions examples/depth_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ int main(int argc, char *argv[])
if (ret)
goto close_session;

enum vaccel_op_type op_type = VACCEL_IMG_DEPTH;
struct vaccel_arg read[2] = { { .size = sizeof(enum vaccel_op_type),
vaccel_op_t op_type = VACCEL_IMG_DEPTH;
struct vaccel_arg read[2] = { { .size = sizeof(vaccel_op_t),
.buf = &op_type },
{ .size = image_size, .buf = image } };
struct vaccel_arg write[1] = {
Expand Down
4 changes: 2 additions & 2 deletions examples/detect_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ int main(int argc, char *argv[])
if (ret)
goto close_session;

enum vaccel_op_type op_type = VACCEL_IMG_DETEC;
struct vaccel_arg read[2] = { { .size = sizeof(enum vaccel_op_type),
vaccel_op_t op_type = VACCEL_IMG_DETEC;
struct vaccel_arg read[2] = { { .size = sizeof(vaccel_op_t),
.buf = &op_type },
{ .size = image_size, .buf = image } };
struct vaccel_arg write[1] = {
Expand Down
2 changes: 1 addition & 1 deletion examples/exec_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(int argc, char **argv)
printf("Initialized session with id: %" PRId64 "\n", sess.id);

input = 10; /* some random input value */

Check warning on line 31 in examples/exec_generic.c

View workflow job for this annotation

GitHub Actions / Validate Code / Lint C/C++ (cpp-linter) (amd64, release)

examples/exec_generic.c:31:10 [readability-magic-numbers]

10 is a magic number; consider replacing it with a named constant
enum vaccel_op_type op_type = VACCEL_EXEC;
vaccel_op_t op_type = VACCEL_EXEC;
struct vaccel_arg read[4] = {
{ .size = sizeof(uint8_t), .buf = &op_type },
{ .size = strlen(argv[1]) + 1, .buf = argv[1] },
Expand Down
4 changes: 2 additions & 2 deletions examples/minmax_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ int main(int argc, char *argv[])
struct timespec t0;
struct timespec t1;

enum vaccel_op_type op_type = VACCEL_MINMAX;
vaccel_op_t op_type = VACCEL_MINMAX;
struct vaccel_arg read[5] = {

Check warning on line 76 in examples/minmax_generic.c

View workflow job for this annotation

GitHub Actions / Validate Code / Lint C/C++ (cpp-linter) (amd64, release)

examples/minmax_generic.c:76:25 [readability-magic-numbers]

5 is a magic number; consider replacing it with a named constant
{ .size = sizeof(enum vaccel_op_type), .buf = &op_type },
{ .size = sizeof(vaccel_op_t), .buf = &op_type },
{ .size = ndata * sizeof(double), .buf = indata },
{ .size = sizeof(int), .buf = &ndata },
{ .size = sizeof(int), .buf = &low_threshold },
Expand Down
4 changes: 2 additions & 2 deletions examples/pose_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ int main(int argc, char *argv[])
if (ret)
goto close_session;

enum vaccel_op_type op_type = VACCEL_IMG_POSE;
struct vaccel_arg read[2] = { { .size = sizeof(enum vaccel_op_type),
vaccel_op_t op_type = VACCEL_IMG_POSE;
struct vaccel_arg read[2] = { { .size = sizeof(vaccel_op_t),
.buf = &op_type },
{ .size = image_size, .buf = image } };
struct vaccel_arg write[1] = {
Expand Down
4 changes: 2 additions & 2 deletions examples/pynq_array_copy_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ int main()

printf("Initialized session with id: %" PRId64 "\n", sess.id);

enum vaccel_op_type op_type = VACCEL_F_ARRAYCOPY;
struct vaccel_arg read[2] = { { .size = sizeof(enum vaccel_op_type),
vaccel_op_t op_type = VACCEL_F_ARRAYCOPY;
struct vaccel_arg read[2] = { { .size = sizeof(vaccel_op_t),
.buf = &op_type },
{ .size = sizeof(a), .buf = (char *)a } };
struct vaccel_arg write[2] = {
Expand Down
4 changes: 2 additions & 2 deletions examples/pynq_parallel_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ int main()

printf("Initialized session with id: %" PRId64 "\n", sess.id);

enum vaccel_op_type op_type = VACCEL_F_PARALLEL;
vaccel_op_t op_type = VACCEL_F_PARALLEL;
struct vaccel_arg read[3] = {
{ .size = sizeof(enum vaccel_op_type), .buf = &op_type },
{ .size = sizeof(vaccel_op_t), .buf = &op_type },
{ .size = sizeof(a), .buf = (char *)a },
{ .size = sizeof(b), .buf = (char *)b },
};
Expand Down
4 changes: 2 additions & 2 deletions examples/pynq_vector_add_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ int main()
}

printf("Initialized session with id: %" PRId64 "\n", sess.id);
enum vaccel_op_type op_type = VACCEL_F_VECTORADD;
struct vaccel_arg read[3] = { { .size = sizeof(enum vaccel_op_type),
vaccel_op_t op_type = VACCEL_F_VECTORADD;
struct vaccel_arg read[3] = { { .size = sizeof(vaccel_op_t),
.buf = &op_type },
{ .size = sizeof(a), .buf = (char *)a },
{ .size = sizeof(b), .buf = (char *)b } };
Expand Down
4 changes: 2 additions & 2 deletions examples/segment_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ int main(int argc, char *argv[])
if (ret)
goto close_session;

enum vaccel_op_type op_type = VACCEL_IMG_SEGME;
struct vaccel_arg read[2] = { { .size = sizeof(enum vaccel_op_type),
vaccel_op_t op_type = VACCEL_IMG_SEGME;
struct vaccel_arg read[2] = { { .size = sizeof(vaccel_op_t),
.buf = &op_type },
{ .size = image_size, .buf = image } };
struct vaccel_arg write[1] = {
Expand Down
4 changes: 2 additions & 2 deletions examples/sgemm_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ int main(int argc, char *argv[])
struct timespec t0;
struct timespec t1;

enum vaccel_op_type op_type = VACCEL_BLAS_SGEMM;
vaccel_op_t op_type = VACCEL_BLAS_SGEMM;
struct vaccel_arg read[8] = {
{ .size = sizeof(enum vaccel_op_type), .buf = &op_type },
{ .size = sizeof(vaccel_op_t), .buf = &op_type },
{ .size = sizeof(int), .buf = &m },
{ .size = sizeof(int), .buf = &n },
{ .size = sizeof(int), .buf = &k },
Expand Down
4 changes: 2 additions & 2 deletions plugins/exec/vaccel.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ struct vaccel_op ops[] = {

static int init(void)
{
return register_plugin_functions(ops, sizeof(ops) / sizeof(ops[0]));
return vaccel_plugin_register_ops(ops, sizeof(ops) / sizeof(ops[0]));
}

static int fini(void)
{
return VACCEL_OK;
}

VACCEL_MODULE(.name = "exec", .version = VACCEL_VERSION,
VACCEL_PLUGIN(.name = "exec", .version = VACCEL_VERSION,
.vaccel_version = VACCEL_VERSION,
.type = VACCEL_PLUGIN_SOFTWARE | VACCEL_PLUGIN_GENERIC |
VACCEL_PLUGIN_CPU,
Expand Down
4 changes: 2 additions & 2 deletions plugins/mbench/vaccel.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct vaccel_op ops[] = {

static int init(void)
{
return register_plugin_functions(ops, sizeof(ops) / sizeof(ops[0]));
return vaccel_plugin_register_ops(ops, sizeof(ops) / sizeof(ops[0]));
}

static int fini(void)
Expand All @@ -83,7 +83,7 @@ static int fini(void)
return VACCEL_OK;
}

VACCEL_MODULE(.name = "mbench", .version = VACCEL_VERSION,
VACCEL_PLUGIN(.name = "mbench", .version = VACCEL_VERSION,
.vaccel_version = VACCEL_VERSION,
.type = VACCEL_PLUGIN_SOFTWARE | VACCEL_PLUGIN_GENERIC |
VACCEL_PLUGIN_CPU,
Expand Down
4 changes: 2 additions & 2 deletions plugins/noop/vaccel.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,14 +743,14 @@ struct vaccel_op ops[] = {

static int init(void)
{
return register_plugin_functions(ops, sizeof(ops) / sizeof(ops[0]));
return vaccel_plugin_register_ops(ops, sizeof(ops) / sizeof(ops[0]));
}

static int fini(void)
{
return VACCEL_OK;
}

VACCEL_MODULE(.name = "noop", .version = VACCEL_VERSION,
VACCEL_PLUGIN(.name = "noop", .version = VACCEL_VERSION,
.vaccel_version = VACCEL_VERSION, .type = VACCEL_PLUGIN_DEBUG,
.init = init, .fini = fini)
Loading

0 comments on commit 1a80696

Please sign in to comment.