summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBui Nguyen Quoc Thanh <thanh.buinguyenquoc@vn.bosch.com>2020-09-23 17:11:54 +0700
committerSaya Sugiura <39760799+ssugiura@users.noreply.github.com>2021-01-06 09:27:28 +0900
commit43c8c18c5130b5d4dfdddcb0b4c61a9155f65b86 (patch)
tree731aa1dda82c1d0f70cab8172366d10bf5a8eac8
parentf374831ed49803d092e3024f0d86090b4e44eb55 (diff)
downloadDLT-daemon-43c8c18c5130b5d4dfdddcb0b4c61a9155f65b86.tar.gz
libdlt: Use poll to avoid CPU high load
Use timeout of polling as time for retry. In case of timeout or failure of polling, continue to close the socket. It could take some times to get the socket is shutdown So it means there could be some data available to read. Try to consume the data and poll the socket again. If read fails, time to close the socket then. Signed-off-by: Bui Nguyen Quoc Thanh <thanh.buinguyenquoc@vn.bosch.com>
-rw-r--r--src/lib/dlt_user.c78
1 files changed, 49 insertions, 29 deletions
diff --git a/src/lib/dlt_user.c b/src/lib/dlt_user.c
index 9f40977..f1c1864 100644
--- a/src/lib/dlt_user.c
+++ b/src/lib/dlt_user.c
@@ -117,8 +117,6 @@ enum StringType
#define DLT_UNUSED(x) (void)(x)
-#define DLT_USER_MAX_RETRY_COUNT 1000
-
/* Network trace */
#ifdef DLT_NETWORK_TRACE_ENABLE
#define DLT_USER_SEGMENTED_THREAD (1<<2)
@@ -961,34 +959,54 @@ DltReturnValue dlt_free(void)
dlt_vlog(LOG_WARNING, "%s: shutdown failed: %s\n", __func__, strerror(errno));
}
else {
- int count = 0;
- while (1) {
- ssize_t bytes_read;
-
- bytes_read = read(dlt_user.dlt_log_handle, dlt_user.resend_buffer, dlt_user.log_buf_len);
+ ssize_t bytes_read = 0;
+ int prev_errno = 0;
+ struct pollfd nfd[1];
+ nfd[0].events = POLLIN;
+ nfd[0].fd = dlt_user.dlt_log_handle;
- if (bytes_read < 0) {
- if (((errno == EAGAIN) || (errno == EWOULDBLOCK)) &&
- count < DLT_USER_MAX_RETRY_COUNT)
- {
- dlt_vlog(LOG_DEBUG, "%s - %d: Retry to read the socket (errno=%d)\n",
- __func__, __LINE__, errno);
-
- count++;
- continue;
- }
- else {
- dlt_vlog(LOG_WARNING,
- "%s - %d: Log messages remain in socket (errno=%d)\n",
- __func__, __LINE__, errno);
- break;
- }
+ while (1) {
+ ret = poll(nfd, 1, DLT_USER_RECEIVE_MDELAY);
+
+ /* In case failure of polling or reaching timeout,
+ * continue to close socket anyway.
+ * */
+ if (ret < 0) {
+ dlt_vlog(LOG_WARNING, "[%s] Failed to poll with error [%s]\n",
+ __func__, strerror(errno));
+ break;
+ }
+ else if (ret == 0) {
+ dlt_vlog(LOG_DEBUG, "[%s] Polling timeout\n", __func__);
+ break;
}
else {
- dlt_vlog(LOG_DEBUG, "%s - %d: %d bytes read from resend buffer\n", __func__, __LINE__, bytes_read);
-
- if (!bytes_read)
- break;
+ /* It could take some time to get the socket is shutdown
+ * So it means there could be some data available to read.
+ * Try to consume the data and poll the socket again.
+ * If read fails, time to close the socket then.
+ */
+ dlt_vlog(LOG_DEBUG, "[%s] polling returns [%d] with revent [0x%x]."
+ "There are something to read\n", __func__, ret, nfd[0].revents);
+
+ bytes_read = read(dlt_user.dlt_log_handle, dlt_user.resend_buffer, dlt_user.log_buf_len);
+ prev_errno = errno;
+
+ if (bytes_read < 0) {
+ dlt_vlog(LOG_WARNING, "[%s] Failed to read with error [%s]\n",
+ __func__, strerror(prev_errno));
+
+ if ((prev_errno == EAGAIN) || (prev_errno == EWOULDBLOCK))
+ continue;
+ else
+ break;
+ }
+ if (bytes_read >= 0) {
+ if (!bytes_read)
+ break;
+ dlt_vlog(LOG_NOTICE, "[%s] data is still readable... [%d] bytes read\n",
+ __func__, bytes_read);
+ }
}
}
}
@@ -1012,9 +1030,11 @@ DltReturnValue dlt_free(void)
dlt_user_free_buffer(&(dlt_user.resend_buffer));
dlt_buffer_free_dynamic(&(dlt_user.startup_buffer));
- DLT_SEM_FREE();
- DLT_SEM_LOCK();
+ /* Clear and free local stored application information */
+ if (dlt_user.application_description != NULL)
+ free(dlt_user.application_description);
+ dlt_user.application_description = NULL;
if (dlt_user.dlt_ll_ts) {
for (i = 0; i < dlt_user.dlt_ll_ts_max_num_entries; i++) {