rawhid-read consumes augmenting cpu time

Status
Not open for further replies.

heimi

Active member
My CNC-App (macos 11.2, Xcode 12.1) sends data to the USB-Device (Teensy3.5) every about 1 sec. The data contains the steps for the next section for the stepper motors. The next data is sent when the teensy has confirmed the end of the section.
That works fine for little tasks. But with bigger tasks, the Interface begins to encrease the amount of CPU persentage. It begins with about 6%, and after 300 sections it engreases up to 40%, sometimes 80%.
Instruments shows no leaks.
On the teensy, I am using the provided Arduino functiuons for ending and receiving data: RawHID.send, recv.
On the mac, I am using the functions of the IOKit/usb and IOKit/hid family, provided by Paul (many years before)

rawhid_recv:

Code:
int rawhid_recv(int num, void *buf, int len, int timeout)
{
   hid_t *hid;
   buffer_t *b;
   CFRunLoopTimerRef timer=NULL;
   CFRunLoopTimerContext context;
   int ret=0, timeout_occurred=0;
   
   if (len < 1) return 0;
   hid = get_hid(num);
   if (!hid || !hid->open) return -1;
   if ((b = hid->first_buffer) != NULL)
   {
      if (len > b->len) len = b->len;
      memcpy(buf, b->buf, len);
      hid->first_buffer = b->next;
      free(b);
      return len;
   }
   memset(&context, 0, sizeof(context));
   context.info = &timeout_occurred;
   if (timeout > 0)
   {
      timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() +(double)timeout / 1000.0, 0, 0, 0, timeout_callback, &context);
      CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
      while (1) {
         CFRunLoopRun();
         if ((b = hid->first_buffer) != NULL) 
         {
            if (len > b->len) len = b->len;
            memcpy(buf, b->buf, len);
            hid->first_buffer = b->next;
            free(b);
            ret = len;
            fprintf(stderr,"rawhid_recv runloop\n");
            break;
         }
         if (!hid->open) {
            //printf("rawhid_recv, device not open\n");
            ret = -1;
            break;
         }
         if (timeout_occurred) break;
      }
      CFRunLoopTimerInvalidate(timer);
      CFRelease(timer);
   }
   //fprintf(stderr,"rawhid_recv ret: %d\n",ret);
   return ret;
}
I call this function with a timer in 0.2s-intervals. Intervals from 0.05s until 0.8s give the same results.
What could lead the app to consume more memory with higher counts of sections?
 
Status
Not open for further replies.
Back
Top