Advertisement
yepp

stress

Apr 29th, 2025
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.17 KB | None | 0 0
  1.  
  2. #include <thread>
  3. #include <chrono>
  4. #include <iostream>
  5. #include <iostream>
  6. #include <thread>
  7. #include <vector>
  8. #include <cmath>
  9. #include <chrono>
  10. #include <atomic>
  11. #include <tracy/Tracy.hpp>
  12. #include <tracy/TracyC.h>
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15. #include <sys/time.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <poll.h>
  20.  
  21. typedef struct {
  22.     int txTrigIrqFd;
  23. } TX_IRQ_HANDLE_S;
  24.  
  25. std::atomic<bool> keepRunning(true);
  26. #define TX_TRIG_IRQ_FILE_A "/dev/uio4"
  27.  
  28. std::atomic<bool> pause_thread;
  29.  
  30. TX_IRQ_HANDLE_S* tx_irq_handler1 = NULL;
  31. TX_IRQ_HANDLE_S* tx_irq_handler2 = NULL;
  32.  
  33. TX_IRQ_HANDLE_S* TX_InitIrq2 ()
  34. {
  35.  
  36.     TX_IRQ_HANDLE_S* pHandle = NULL;
  37.     pHandle = (TX_IRQ_HANDLE_S*) ( malloc (sizeof (TX_IRQ_HANDLE_S)) );
  38.     if (!pHandle)
  39.     {
  40.         return NULL;
  41.     }
  42.  
  43.     /* Open irq device */
  44.     pHandle->txTrigIrqFd = open (TX_TRIG_IRQ_FILE_A, (O_RDWR));
  45.     if (pHandle->txTrigIrqFd < 0)
  46.     {
  47.         fprintf (stderr, "*** ERROR: Could not open device %s (%s).\n", TX_TRIG_IRQ_FILE_A, strerror (-errno));
  48.         free (pHandle);
  49.         return NULL;
  50.     }
  51.  
  52.     return pHandle;
  53. }
  54.  
  55. bool TX_ResetIrq2 (TX_IRQ_HANDLE_S* pHandle)
  56. {
  57.     unsigned int irqRstVal = 1;
  58.     if (!pHandle || pHandle->txTrigIrqFd < 0)
  59.     {
  60.         return false;
  61.     }
  62.  
  63.     /* Reset interrupt */
  64.     if (write (pHandle->txTrigIrqFd, &irqRstVal, sizeof(irqRstVal)) < 0)
  65.     {
  66.         fprintf (stderr, "*** ERROR: Problem clearing device interrupt (%s).\n", strerror (errno));
  67.         return false;
  68.     }
  69.  
  70.     return true;
  71. }
  72.  
  73. bool TX_WaitIrq2 (TX_IRQ_HANDLE_S* pHandle, unsigned int timeOutMs)
  74. {
  75.     TracyCZone(ctx, 1);
  76.     struct timeval timeout;
  77.     struct timeval *pTimeout = &timeout;
  78.     fd_set set;
  79.     int status;
  80.     int32_t irqCount;
  81.  
  82.     if (!pHandle || pHandle->txTrigIrqFd < 0)
  83.     {
  84.         TracyCZoneEnd(ctx);
  85.         return false;
  86.     }
  87.  
  88.        
  89.     // uint32_t info;
  90.     // ssize_t nb = read(pHandle->txTrigIrqFd, &info, sizeof(info)); // Read the interrupt info
  91.     // if (nb != sizeof(info)) {
  92.     //     std::cerr << "ERROR: Read failed" << std::endl;
  93.     //     TracyCZoneEnd(ctx);
  94.     //     return ErrCodeToStatus (-errno);
  95.     // }
  96.    
  97.     // std::cout << "Interrupt received, processing..." << std::endl;
  98.    
  99.     // // After processing the interrupt, acknowledge it by writing back to the UIO device
  100.     // nb = write(pHandle->txTrigIrqFd, &info, sizeof(info)); // Acknowledge interrupt
  101.     // if (nb != sizeof(info)) {
  102.     //     std::cerr << "ERROR: Write failed" << std::endl;
  103.     //     TracyCZoneEnd(ctx);
  104.     //     return ErrCodeToStatus (-errno);
  105.     // }
  106.    
  107.     // TracyCZoneEnd(ctx);
  108.     // return comm_status_ok;
  109.  
  110.  
  111.     struct pollfd pfd;
  112.     pfd.fd = pHandle->txTrigIrqFd;
  113.     pfd.events = POLLIN;
  114.  
  115.     TracyCZoneN(ctx2,"Polling",true)
  116.     int ret = poll(&pfd, 1, -1); // -1 = block indefinitely
  117.     TracyCZoneEnd(ctx2)
  118.  
  119.     if (ret < 0) {
  120.         fprintf (stderr, "*** ERROR: YOLO (%s).\n", strerror (errno));
  121.         TracyCZoneEnd(ctx);
  122.         return false;
  123.     }
  124.        
  125.         if (pfd.revents & POLLIN)  {
  126.             TracyCZoneN(ctx2,"Reading",true)  
  127.             if (read (pHandle->txTrigIrqFd, &irqCount, sizeof(irqCount)) < 0) {
  128.                 fprintf (stderr, "*** ERROR: Problem reading interrupt count (%s).\n", strerror (errno));
  129.                 TracyCZoneEnd(ctx);
  130.                 return false;
  131.             }
  132.             TracyCZoneEnd(ctx2)
  133.         } else {
  134.             fprintf (stderr, "*** ERROR: IZE (%s).\n", strerror (errno));
  135.             TracyCZoneEnd(ctx);
  136.             return false;
  137.         }  
  138.  
  139.         TX_ResetIrq2(tx_irq_handler1);
  140.         TracyCZoneEnd(ctx);
  141.         return true;
  142.    
  143.  
  144.     // uint32_t info = 1; /* unmask */
  145.  
  146.     // TracyCZoneN(ctx2,"Write",true)
  147.     // ssize_t nb = write(pHandle->txTrigIrqFd, &info, sizeof(info));
  148.     // if (nb != (ssize_t)sizeof(info)) {
  149.     //     fprintf (stderr, "*** ERROR: Problem writing interrupt count (%s).\n", strerror (errno));
  150.     //     TracyCZoneEnd(ctx2);
  151.     //     TracyCZoneEnd(ctx);
  152.  
  153.     //     return ErrCodeToStatus (-errno);
  154.     // }
  155.     // TracyCZoneEnd(ctx2);
  156.  
  157.     // /* Wait for interrupt */
  158.     // TracyCZoneN(ctx3,"Read",true)
  159.     // nb = read(pHandle->txTrigIrqFd, &info, sizeof(info));
  160.     // if (nb == (ssize_t)sizeof(info)) {
  161.     //     TracyCZoneEnd(ctx3);
  162.     //     TracyCZoneEnd(ctx);
  163.     //     return comm_status_ok;
  164.     // }
  165.     // TracyCZoneEnd(ctx3);
  166.    
  167.     // uint32_t info_disable = 0; /* unmask */
  168.     // write(pHandle->txTrigIrqFd, &info_disable, sizeof(info_disable));
  169.    
  170.     // TracyCZoneEnd(ctx);
  171.     // return comm_status_ok;
  172. }
  173.  
  174. // uint32_t get_ping_number()
  175. // {
  176. //     ZoneScoped;
  177. //     uint32_t ping;
  178. //     auto status = TX_GetIrqNumber(&ping);
  179. //     if (status != true) {
  180. //         std::cout << "get irq number failed";
  181. //     }
  182. //     TracyPlot("ping_number", static_cast<float>(ping));
  183. //     return ping;
  184. // }
  185.  
  186. // Function to simulate CPU load
  187. void cpuStressWorker(TX_IRQ_HANDLE_S* handler) {
  188.     while (keepRunning.load()) {
  189.         volatile double x = 0.0001;
  190.         for (int i = 0; i < 20000; ++i) {
  191.             x += std::sin(x) * std::cos(x);
  192.         }
  193.         TracyCZoneN(ctx,"WAIT_IRQ",true)
  194.         auto status = TX_WaitIrq2(handler, 2000);
  195.         TracyCZoneEnd(ctx)
  196.  
  197.         long sleeptime = rand() / (float)RAND_MAX * 3 + 1;
  198.         // if (status != comm_status_ok && status != comm_status_timeout) {
  199.         //     std::cout << "wait for irq failed" <<std::endl;
  200.         // }
  201.         uint32_t ping_at_process_end = 1;//;get_ping_number();
  202.         //std::this_thread::sleep_for(std::chrono::milliseconds(sleeptime)); // small sleep to prevent full load
  203.         const bool current = pause_thread.exchange(false);
  204.         if (current) {
  205.             std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // small sleep to prevent full load  
  206.         }
  207.         std::cout << "A ping number is " << ping_at_process_end << " , slept " << sleeptime << " ms" << std::endl;
  208.     }
  209. }
  210.  
  211. int main(int argc, char* argv[]) {
  212.  
  213.     pause_thread = false;
  214.     tx_irq_handler1 = TX_InitIrq2();
  215.     if (!tx_irq_handler1) {
  216.         std::cout << "irw handler init failed ";
  217.         return 1;
  218.     }
  219.  
  220.     tx_irq_handler2 = TX_InitIrq2();
  221.     if (!tx_irq_handler2) {
  222.         std::cout << "irw handler init failed ";
  223.         return 1;
  224.     }
  225.  
  226.     std::cout << "Starting 2 threads...\n";
  227.  
  228.     std::vector<std::thread> threads;
  229.  
  230.     TX_ResetIrq2(tx_irq_handler1);
  231.     threads.emplace_back(cpuStressWorker, tx_irq_handler1 );
  232.     //threads.emplace_back(cpuStressWorker, tx_irq_handler2);
  233.    
  234.     char input;
  235.     while (true) {
  236.         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  237.         std::cin >> input;
  238.  
  239.         if (input == 'a') {
  240.             std::cout << "------- PAUSING for 1 sec" << std::endl;
  241.             pause_thread = true;
  242.         } else {
  243.             std::cout << "-------NOT SUPPORTED" << std::endl;
  244.         }
  245.     }
  246.  
  247.     keepRunning = false;
  248.  
  249.     for (auto& t : threads) {
  250.         if (t.joinable()) {
  251.             t.join();
  252.         }
  253.     }
  254.  
  255.     std::cout << "Stopped.\n";
  256.     return 0;
  257. }
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement