qos_external.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * libqos driver framework
  3. *
  4. * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License version 2.1 as published by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  17. */
  18. #include "qemu/osdep.h"
  19. #include <getopt.h>
  20. #include "../libqtest.h"
  21. #include "qapi/qmp/qdict.h"
  22. #include "qapi/qmp/qbool.h"
  23. #include "qapi/qmp/qstring.h"
  24. #include "qemu/module.h"
  25. #include "qapi/qmp/qlist.h"
  26. #include "malloc.h"
  27. #include "qgraph.h"
  28. #include "qgraph_internal.h"
  29. #include "qos_external.h"
  30. static void machine_apply_to_node(const char *name)
  31. {
  32. char *machine_name = g_strconcat(qtest_get_arch(), "/", name, NULL);
  33. qos_graph_node_set_availability(machine_name, true);
  34. g_free(machine_name);
  35. }
  36. void machines_apply_to_node(MachineInfoList *mach_info)
  37. {
  38. MachineInfoList *tail;
  39. for (tail = mach_info; tail; tail = tail->next) {
  40. machine_apply_to_node(tail->value->name);
  41. if (tail->value->alias) {
  42. machine_apply_to_node(tail->value->alias);
  43. }
  44. }
  45. }
  46. static void type_apply_to_node(const char *name, bool is_abstract)
  47. {
  48. qos_graph_node_set_availability(name, true);
  49. if (is_abstract) {
  50. qos_delete_cmd_line(name);
  51. }
  52. }
  53. void types_apply_to_node(ObjectTypeInfoList *type_info)
  54. {
  55. ObjectTypeInfoList *tail;
  56. for (tail = type_info; tail; tail = tail->next) {
  57. type_apply_to_node(tail->value->name, tail->value->abstract);
  58. }
  59. }
  60. static QGuestAllocator *get_machine_allocator(QOSGraphObject *obj)
  61. {
  62. return obj->get_driver(obj, "memory");
  63. }
  64. /**
  65. * allocate_objects(): given an array of nodes @arg,
  66. * walks the path invoking all constructors and
  67. * passing the corresponding parameter in order to
  68. * continue the objects allocation.
  69. * Once the test is reached, return the object it consumes.
  70. *
  71. * Since the machine and QEDGE_CONSUMED_BY nodes allocate
  72. * memory in the constructor, g_test_queue_destroy is used so
  73. * that after execution they can be safely free'd. (The test's
  74. * ->before callback is also welcome to use g_test_queue_destroy).
  75. *
  76. * Note: as specified in walk_path() too, @arg is an array of
  77. * char *, where arg[0] is a pointer to the command line
  78. * string that will be used to properly start QEMU when executing
  79. * the test, and the remaining elements represent the actual objects
  80. * that will be allocated.
  81. */
  82. void *allocate_objects(QTestState *qts, char **path, QGuestAllocator **p_alloc)
  83. {
  84. int current = 0;
  85. QGuestAllocator *alloc;
  86. QOSGraphObject *parent = NULL;
  87. QOSGraphEdge *edge;
  88. QOSGraphNode *node;
  89. void *edge_arg;
  90. void *obj;
  91. node = qos_graph_get_node(path[current]);
  92. g_assert(node->type == QNODE_MACHINE);
  93. obj = qos_machine_new(node, qts);
  94. qos_object_queue_destroy(obj);
  95. alloc = get_machine_allocator(obj);
  96. if (p_alloc) {
  97. *p_alloc = alloc;
  98. }
  99. for (;;) {
  100. if (node->type != QNODE_INTERFACE) {
  101. qos_object_start_hw(obj);
  102. parent = obj;
  103. }
  104. /* follow edge and get object for next node constructor */
  105. current++;
  106. edge = qos_graph_get_edge(path[current - 1], path[current]);
  107. node = qos_graph_get_node(path[current]);
  108. if (node->type == QNODE_TEST) {
  109. g_assert(qos_graph_edge_get_type(edge) == QEDGE_CONSUMED_BY);
  110. return obj;
  111. }
  112. switch (qos_graph_edge_get_type(edge)) {
  113. case QEDGE_PRODUCES:
  114. obj = parent->get_driver(parent, path[current]);
  115. break;
  116. case QEDGE_CONSUMED_BY:
  117. edge_arg = qos_graph_edge_get_arg(edge);
  118. obj = qos_driver_new(node, obj, alloc, edge_arg);
  119. qos_object_queue_destroy(obj);
  120. break;
  121. case QEDGE_CONTAINS:
  122. obj = parent->get_device(parent, path[current]);
  123. break;
  124. }
  125. }
  126. }