Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <thread>
- #include <chrono>
- #include <iostream>
- #include <iostream>
- #include <thread>
- #include <vector>
- #include <cmath>
- #include <chrono>
- #include <atomic>
- #include <tracy/Tracy.hpp>
- #include <tracy/TracyC.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <poll.h>
- typedef struct {
- int txTrigIrqFd;
- } TX_IRQ_HANDLE_S;
- std::atomic<bool> keepRunning(true);
- #define TX_TRIG_IRQ_FILE_A "/dev/uio4"
- std::atomic<bool> pause_thread;
- TX_IRQ_HANDLE_S* tx_irq_handler1 = NULL;
- TX_IRQ_HANDLE_S* tx_irq_handler2 = NULL;
- TX_IRQ_HANDLE_S* TX_InitIrq2 ()
- {
- TX_IRQ_HANDLE_S* pHandle = NULL;
- pHandle = (TX_IRQ_HANDLE_S*) ( malloc (sizeof (TX_IRQ_HANDLE_S)) );
- if (!pHandle)
- {
- return NULL;
- }
- /* Open irq device */
- pHandle->txTrigIrqFd = open (TX_TRIG_IRQ_FILE_A, (O_RDWR));
- if (pHandle->txTrigIrqFd < 0)
- {
- fprintf (stderr, "*** ERROR: Could not open device %s (%s).\n", TX_TRIG_IRQ_FILE_A, strerror (-errno));
- free (pHandle);
- return NULL;
- }
- return pHandle;
- }
- bool TX_ResetIrq2 (TX_IRQ_HANDLE_S* pHandle)
- {
- unsigned int irqRstVal = 1;
- if (!pHandle || pHandle->txTrigIrqFd < 0)
- {
- return false;
- }
- /* Reset interrupt */
- if (write (pHandle->txTrigIrqFd, &irqRstVal, sizeof(irqRstVal)) < 0)
- {
- fprintf (stderr, "*** ERROR: Problem clearing device interrupt (%s).\n", strerror (errno));
- return false;
- }
- return true;
- }
- bool TX_WaitIrq2 (TX_IRQ_HANDLE_S* pHandle, unsigned int timeOutMs)
- {
- TracyCZone(ctx, 1);
- struct timeval timeout;
- struct timeval *pTimeout = &timeout;
- fd_set set;
- int status;
- int32_t irqCount;
- if (!pHandle || pHandle->txTrigIrqFd < 0)
- {
- TracyCZoneEnd(ctx);
- return false;
- }
- // uint32_t info;
- // ssize_t nb = read(pHandle->txTrigIrqFd, &info, sizeof(info)); // Read the interrupt info
- // if (nb != sizeof(info)) {
- // std::cerr << "ERROR: Read failed" << std::endl;
- // TracyCZoneEnd(ctx);
- // return ErrCodeToStatus (-errno);
- // }
- // std::cout << "Interrupt received, processing..." << std::endl;
- // // After processing the interrupt, acknowledge it by writing back to the UIO device
- // nb = write(pHandle->txTrigIrqFd, &info, sizeof(info)); // Acknowledge interrupt
- // if (nb != sizeof(info)) {
- // std::cerr << "ERROR: Write failed" << std::endl;
- // TracyCZoneEnd(ctx);
- // return ErrCodeToStatus (-errno);
- // }
- // TracyCZoneEnd(ctx);
- // return comm_status_ok;
- struct pollfd pfd;
- pfd.fd = pHandle->txTrigIrqFd;
- pfd.events = POLLIN;
- TracyCZoneN(ctx2,"Polling",true)
- int ret = poll(&pfd, 1, -1); // -1 = block indefinitely
- TracyCZoneEnd(ctx2)
- if (ret < 0) {
- fprintf (stderr, "*** ERROR: YOLO (%s).\n", strerror (errno));
- TracyCZoneEnd(ctx);
- return false;
- }
- if (pfd.revents & POLLIN) {
- TracyCZoneN(ctx2,"Reading",true)
- if (read (pHandle->txTrigIrqFd, &irqCount, sizeof(irqCount)) < 0) {
- fprintf (stderr, "*** ERROR: Problem reading interrupt count (%s).\n", strerror (errno));
- TracyCZoneEnd(ctx);
- return false;
- }
- TracyCZoneEnd(ctx2)
- } else {
- fprintf (stderr, "*** ERROR: IZE (%s).\n", strerror (errno));
- TracyCZoneEnd(ctx);
- return false;
- }
- TX_ResetIrq2(tx_irq_handler1);
- TracyCZoneEnd(ctx);
- return true;
- // uint32_t info = 1; /* unmask */
- // TracyCZoneN(ctx2,"Write",true)
- // ssize_t nb = write(pHandle->txTrigIrqFd, &info, sizeof(info));
- // if (nb != (ssize_t)sizeof(info)) {
- // fprintf (stderr, "*** ERROR: Problem writing interrupt count (%s).\n", strerror (errno));
- // TracyCZoneEnd(ctx2);
- // TracyCZoneEnd(ctx);
- // return ErrCodeToStatus (-errno);
- // }
- // TracyCZoneEnd(ctx2);
- // /* Wait for interrupt */
- // TracyCZoneN(ctx3,"Read",true)
- // nb = read(pHandle->txTrigIrqFd, &info, sizeof(info));
- // if (nb == (ssize_t)sizeof(info)) {
- // TracyCZoneEnd(ctx3);
- // TracyCZoneEnd(ctx);
- // return comm_status_ok;
- // }
- // TracyCZoneEnd(ctx3);
- // uint32_t info_disable = 0; /* unmask */
- // write(pHandle->txTrigIrqFd, &info_disable, sizeof(info_disable));
- // TracyCZoneEnd(ctx);
- // return comm_status_ok;
- }
- // uint32_t get_ping_number()
- // {
- // ZoneScoped;
- // uint32_t ping;
- // auto status = TX_GetIrqNumber(&ping);
- // if (status != true) {
- // std::cout << "get irq number failed";
- // }
- // TracyPlot("ping_number", static_cast<float>(ping));
- // return ping;
- // }
- // Function to simulate CPU load
- void cpuStressWorker(TX_IRQ_HANDLE_S* handler) {
- while (keepRunning.load()) {
- volatile double x = 0.0001;
- for (int i = 0; i < 20000; ++i) {
- x += std::sin(x) * std::cos(x);
- }
- TracyCZoneN(ctx,"WAIT_IRQ",true)
- auto status = TX_WaitIrq2(handler, 2000);
- TracyCZoneEnd(ctx)
- long sleeptime = rand() / (float)RAND_MAX * 3 + 1;
- // if (status != comm_status_ok && status != comm_status_timeout) {
- // std::cout << "wait for irq failed" <<std::endl;
- // }
- uint32_t ping_at_process_end = 1;//;get_ping_number();
- //std::this_thread::sleep_for(std::chrono::milliseconds(sleeptime)); // small sleep to prevent full load
- const bool current = pause_thread.exchange(false);
- if (current) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // small sleep to prevent full load
- }
- std::cout << "A ping number is " << ping_at_process_end << " , slept " << sleeptime << " ms" << std::endl;
- }
- }
- int main(int argc, char* argv[]) {
- pause_thread = false;
- tx_irq_handler1 = TX_InitIrq2();
- if (!tx_irq_handler1) {
- std::cout << "irw handler init failed ";
- return 1;
- }
- tx_irq_handler2 = TX_InitIrq2();
- if (!tx_irq_handler2) {
- std::cout << "irw handler init failed ";
- return 1;
- }
- std::cout << "Starting 2 threads...\n";
- std::vector<std::thread> threads;
- TX_ResetIrq2(tx_irq_handler1);
- threads.emplace_back(cpuStressWorker, tx_irq_handler1 );
- //threads.emplace_back(cpuStressWorker, tx_irq_handler2);
- char input;
- while (true) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1000));
- std::cin >> input;
- if (input == 'a') {
- std::cout << "------- PAUSING for 1 sec" << std::endl;
- pause_thread = true;
- } else {
- std::cout << "-------NOT SUPPORTED" << std::endl;
- }
- }
- keepRunning = false;
- for (auto& t : threads) {
- if (t.joinable()) {
- t.join();
- }
- }
- std::cout << "Stopped.\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement