g2d_sink_test.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /* auto generate by HHB_VERSION "1.13.x" */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include <libgen.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <sys/mman.h>
  28. #include <pthread.h>
  29. #include <memory.h>
  30. #include "io.h"
  31. #include "shl_ref.h"
  32. #include "process_linker_types.h"
  33. #include "process_linker.h"
  34. #define FILE_LENGTH 1028
  35. #ifndef NULL
  36. #define NULL ((void *)0)
  37. #endif
  38. #define NUM_OF_BUFFERS 5
  39. #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
  40. } while (0)
  41. int main(int argc, char **argv) {
  42. PlinkStatus sts = PLINK_STATUS_OK;
  43. PlinkPacket sendpkt, recvpkt;
  44. PlinkMsg msg;
  45. PlinkHandle plink = NULL;
  46. FILE *fp = NULL;
  47. int exitcode = 0;
  48. int i,j;
  49. uint64_t start_time, end_time;
  50. uint64_t _start_time, _end_time;
  51. int frames = 1000;
  52. int fd_mem = open("/dev/mem", O_RDWR | O_SYNC);
  53. if (fd_mem == -1) {
  54. printf("ERROR: failed to open: %s\n", "/dev/mem");
  55. return -1;
  56. }
  57. if (PLINK_create(&plink, "/tmp/plink_npu_featuremap.test", PLINK_MODE_CLIENT) != PLINK_STATUS_OK)
  58. errExit("Failed to create PLINK.");
  59. if (PLINK_connect(plink, NULL) != PLINK_STATUS_OK)
  60. errExit("Failed to connect to server.");
  61. int frmcnt = 0;
  62. // while loop to receive shm features
  63. do {
  64. sts = PLINK_recv(plink, 0, &recvpkt);
  65. for (i = 0; i < recvpkt.num; i++) {
  66. PlinkDescHdr *hdr = (PlinkDescHdr *)(recvpkt.list[i]);
  67. if (hdr->type == PLINK_TYPE_OBJECT) {
  68. PlinkObjectInfo *info = (PlinkObjectInfo *)(recvpkt.list[i]);
  69. printf("[G2D CLIENT] Received frame<%d> addr:<0x%08x>: face_cnt<%d>\n",
  70. info->header.id, info->bus_address, info->object_cnt);
  71. /* set input */
  72. char filename[FILE_LENGTH] = {0};
  73. uint8_t *vaddr = 0;
  74. start_time = shl_get_timespec();
  75. //printf("g2d_sink_test: wait frame%d\n", frmcnt);
  76. int size = info->object_cnt * sizeof(PlinkObjectDetect);
  77. vaddr = (uint8_t *) mmap(0, size, PROT_READ | PROT_WRITE,
  78. MAP_SHARED, fd_mem, info->bus_address);
  79. //printf("g2d_sink_test: get feature frame %d\n", index);
  80. end_time = shl_get_timespec();
  81. //printf("wait feature frame time: %.5fmsf\n", ((float)(end_time-start_time))/1000000);
  82. PlinkObjectDetect *face = (PlinkObjectDetect *)vaddr;
  83. for (j = 0; j < info->object_cnt; j++) {
  84. printf("feat[%d]: score<%f>, [%f,%f]~[%f,%f]\n", j,
  85. face[j].score,
  86. face[j].box.x1, face[j].box.y1,
  87. face[j].box.x2, face[j].box.y2);
  88. }
  89. // return the buffer to source
  90. msg.header.type = PLINK_TYPE_MESSAGE;
  91. msg.header.size = DATA_SIZE(PlinkMsg);
  92. msg.msg = hdr->id;
  93. sendpkt.list[0] = &msg;
  94. sendpkt.num = 1;
  95. sendpkt.fd = PLINK_INVALID_FD;
  96. if (PLINK_send(plink, 0, &sendpkt) == PLINK_STATUS_ERROR)
  97. errExit("Failed to send data.");
  98. //printf("g2d_sink_test: release frame%d\n", frmcnt);
  99. fprintf(stderr, "Run G2D frame time: %.5fms, FPS=%.2f\n", ((float)(end_time-start_time))/1000000,
  100. 1000000000.0/((float)(end_time-start_time)));
  101. }
  102. else if (hdr->type == PLINK_TYPE_MESSAGE)
  103. {
  104. PlinkMsg *msg = (PlinkMsg *)(recvpkt.list[i]);
  105. if (msg->msg == PLINK_EXIT_CODE)
  106. {
  107. exitcode = 1;
  108. printf("Exit\n");
  109. break;
  110. }
  111. }
  112. }
  113. if (recvpkt.fd != PLINK_INVALID_FD)
  114. close(recvpkt.fd);
  115. frmcnt++;
  116. #if 0
  117. if (frmcnt >= frames)
  118. {
  119. msg.header.type = PLINK_TYPE_MESSAGE;
  120. msg.header.size = DATA_SIZE(PlinkMsg);
  121. msg.msg = PLINK_EXIT_CODE;
  122. sendpkt.list[0] = &msg;
  123. sendpkt.num = 1;
  124. sendpkt.fd = PLINK_INVALID_FD;
  125. if (PLINK_send(plink, 0, &sendpkt) == PLINK_STATUS_ERROR)
  126. errExit("Failed to send data.");
  127. break;
  128. }
  129. #endif
  130. } while (exitcode == 0);
  131. cleanup:
  132. sleep(1); // Sleep one second to make sure server is ready for exit
  133. PLINK_close(plink, 0);
  134. if (fp != NULL)
  135. fclose(fp);
  136. return 0;
  137. }