table.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <iostream>
  2. #include <phpcpp.h>
  3. #include "bpf_module.h"
  4. #include "BPF.h"
  5. class BaseTable : public Php::Base {
  6. public:
  7. ebpf::BPF *bpf;
  8. std::string tb_name;
  9. BaseTable(ebpf::BPF *bpf_instance, const std::string &tb_name)
  10. : bpf(bpf_instance), tb_name(tb_name) {}
  11. virtual ~BaseTable() = default;
  12. void printName() { std::cout << "Table name: " << tb_name << std::endl; }
  13. };
  14. class PerfEventArrayTable : public BaseTable {
  15. private:
  16. std::string cb;
  17. public:
  18. using BaseTable::BaseTable;
  19. // ebpf::BPF *bpf;
  20. // std::string event_name;
  21. //
  22. // PerfEventArrayTable(ebpf::BPF *bpf_instance, const std::string &event_name)
  23. // : bpf(bpf_instance), event_name(event_name) {}
  24. // using BaseTable::BaseTable;
  25. virtual ~PerfEventArrayTable() = default;
  26. static void callbackfn(void *cookie, void *data, int data_size) {
  27. auto *instance = static_cast<PerfEventArrayTable *>(cookie);
  28. if (!instance) return;
  29. Php::Value phpData((const char *) data, data_size);
  30. Php::call(instance->cb.c_str(), nullptr, phpData, data_size);
  31. }
  32. void php_open_perf_buffer(Php::Parameters &params) {
  33. this->cb = params[0].stringValue();
  34. auto res = this->bpf->open_perf_buffer(this->tb_name, callbackfn, nullptr, this);
  35. if (!res.ok()) {
  36. throw Php::Exception("open_perf_buffer error:" + res.msg());
  37. }
  38. // auto control_table_hdl = bpf->get_array_table<uint64_t>("counters");
  39. // uint64_t val;
  40. // auto res1 = control_table_hdl.get_value(0, val);
  41. // Php::out << "ok]" << res1.ok() << std::endl;
  42. // Php::out << "msg]" << res1.msg() << std::endl;
  43. // Php::out << "val]" << val << std::endl;
  44. }
  45. int perf_buffer_poll(int timeout_ms) {
  46. return bpf->poll_perf_buffer(this->tb_name, timeout_ms); // perf_reader_event_read
  47. }
  48. };
  49. class HashTable : public BaseTable {
  50. public:
  51. using BaseTable::BaseTable;
  52. };
  53. class ArrayTable : public BaseTable {
  54. public:
  55. using BaseTable::BaseTable;
  56. Php::Value Value(Php::Parameters &param) {
  57. auto index = param[0].numericValue();
  58. // todo val type
  59. uint64_t val;
  60. auto table = bpf->get_array_table<uint64_t>(tb_name);
  61. auto res = table.get_value(index, val);
  62. if (!res.ok()) {
  63. throw Php::Exception("Get value error in" + std::string(tb_name));
  64. }
  65. Php::Value phpValue = static_cast<int64_t>(val);
  66. return phpValue;
  67. }
  68. };
  69. class ProgArrayTable : public BaseTable {
  70. public:
  71. using BaseTable::BaseTable;
  72. void Value(Php::Parameters &param) {
  73. auto index = param[0].numericValue();
  74. // todo key type
  75. uint64_t val;
  76. auto table = bpf->get_array_table<uint64_t>(tb_name);
  77. auto res = table.get_value(index, val);
  78. printf("%d\n", val);
  79. }
  80. };
  81. class PerCpuHashTable : public BaseTable {
  82. public:
  83. using BaseTable::BaseTable;
  84. };
  85. class PerCpuArrayTable : public BaseTable {
  86. public:
  87. using BaseTable::BaseTable;
  88. };
  89. class LpmTrieTable : public BaseTable {
  90. public:
  91. using BaseTable::BaseTable;
  92. };
  93. class StackTraceTable : public BaseTable {
  94. public:
  95. using BaseTable::BaseTable;
  96. };
  97. class LruHashTable : public BaseTable {
  98. public:
  99. using BaseTable::BaseTable;
  100. };
  101. class LruPerCpuHashTable : public BaseTable {
  102. public:
  103. using BaseTable::BaseTable;
  104. };
  105. class CgroupArrayTable : public BaseTable {
  106. public:
  107. using BaseTable::BaseTable;
  108. };
  109. class DevMapTable : public BaseTable {
  110. public:
  111. using BaseTable::BaseTable;
  112. };
  113. class CpuMapTable : public BaseTable {
  114. public:
  115. using BaseTable::BaseTable;
  116. };
  117. class XskMapTable : public BaseTable {
  118. public:
  119. using BaseTable::BaseTable;
  120. };
  121. class MapInMapArrayTable : public BaseTable {
  122. public:
  123. using BaseTable::BaseTable;
  124. };
  125. class MapInMapHashTable : public BaseTable {
  126. public:
  127. using BaseTable::BaseTable;
  128. };
  129. class QueueStackTable : public BaseTable {
  130. public:
  131. using BaseTable::BaseTable;
  132. };
  133. class RingBufTable : public BaseTable {
  134. public:
  135. using BaseTable::BaseTable;
  136. };