Skip to content

Commit

Permalink
tools/nut-scanner/scan_*.c: trace parallel scan ability init [network…
Browse files Browse the repository at this point in the history
…upstools#2511]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Jul 8, 2024
1 parent 74428ec commit d847727
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
2 changes: 1 addition & 1 deletion tools/nut-scanner/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ libnutscan_la_LDFLAGS += -version-info 2:5:2
# copies of "nut_debug_level" making fun of our debug-logging attempts.
# One solution to tackle if needed for those cases would be to make some
# dynamic/shared libnutcommon (etc.)
libnutscan_la_LDFLAGS += -export-symbols-regex '^(nutscan_|nut_debug_level|s_upsdebugx|fatalx|xcalloc|snprintfcat|max_threads|curr_threads|nut_report_config_flags|upsdebugx_report_search_paths|nut_prepare_search_paths)'
libnutscan_la_LDFLAGS += -export-symbols-regex '^(nutscan_|nut_debug_level|s_upsdebug|fatalx|xcalloc|snprintfcat|max_threads|curr_threads|nut_report_config_flags|upsdebugx_report_search_paths|nut_prepare_search_paths)'
libnutscan_la_CFLAGS = -I$(top_srcdir)/clients -I$(top_srcdir)/include \
$(LIBLTDL_CFLAGS) -I$(top_srcdir)/drivers

Expand Down
4 changes: 3 additions & 1 deletion tools/nut-scanner/nut-scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,9 @@ int main(int argc, char *argv[])
}

upsdebugx(1, "Parallel scan support: max_threads=%" PRIuSIZE, max_threads);
sem_init(current_sem, 0, (unsigned int)max_threads);
if (sem_init(current_sem, 0, (unsigned int)max_threads)) {
upsdebug_with_errno(4, "Parallel scan support: sem_init() failed");
}
# endif
#else
upsdebugx(1, "Parallel scan support: !HAVE_PTHREAD");
Expand Down
8 changes: 7 additions & 1 deletion tools/nut-scanner/nutscan-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <string.h>
#include "nut-scan.h"
#include "nut_platform.h"
#include "nut_stdint.h"

#ifdef WIN32
# define SOEXT ".dll"
Expand Down Expand Up @@ -159,7 +160,12 @@ void nutscan_init(void)
__func__);
max_threads = UINT_MAX - 1;
}
sem_init(&semaphore, 0, (unsigned int)max_threads);

upsdebugx(1, "%s: Parallel scan support: max_threads=%" PRIuSIZE,
__func__, max_threads);
if (sem_init(&semaphore, 0, (unsigned int)max_threads)) {
upsdebug_with_errno(4, "%s: Parallel scan support: sem_init() failed", __func__);
}
# endif

# ifdef HAVE_PTHREAD_TRYJOIN
Expand Down
28 changes: 23 additions & 5 deletions tools/nut-scanner/scan_nut.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ nutscan_device_t * nutscan_scan_nut(const char* startIP, const char* stopIP, con
__func__);
max_threads_scantype = UINT_MAX - 1;
}
sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype);

upsdebugx(4, "%s: sem_init() for %" PRIuSIZE " threads", __func__, max_threads_scantype);
if (sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype)) {
upsdebug_with_errno(4, "%s: sem_init() failed", __func__);
}
}
# endif /* HAVE_SEMAPHORE */

Expand Down Expand Up @@ -356,8 +360,21 @@ nutscan_device_t * nutscan_scan_nut(const char* startIP, const char* stopIP, con
sem_wait(semaphore);
pass = TRUE;
} else {
pass = ((max_threads_scantype == 0 || sem_trywait(semaphore_scantype) == 0) &&
sem_trywait(semaphore) == 0);
/* If successful (the lock was acquired),
* sem_wait() and sem_trywait() will return 0.
* Otherwise, -1 is returned and errno is set,
* and the state of the semaphore is unchanged.
*/
int stwST = sem_trywait(semaphore_scantype), stwS = sem_trywait(semaphore);
pass = ((max_threads_scantype == 0 || stwST == 0) && stwS == 0);
upsdebugx(4, "%s: max_threads_scantype=%" PRIuSIZE
" curr_threads=%" PRIuSIZE
" thread_count=%" PRIuSIZE
" stwST=%d stwS=%d pass=%d",
__func__, max_threads_scantype,
curr_threads, thread_count,
stwST, stwS, pass
);
}
# else
# ifdef HAVE_PTHREAD_TRYJOIN
Expand Down Expand Up @@ -494,9 +511,10 @@ nutscan_device_t * nutscan_scan_nut(const char* startIP, const char* stopIP, con
# ifdef HAVE_SEMAPHORE
/* Wait for all current scans to complete */
if (thread_array != NULL) {
upsdebugx (2, "%s: Running too many scanning threads, "
upsdebugx (2, "%s: Running too many scanning threads (%"
PRIuSIZE "), "
"waiting until older ones would finish",
__func__);
__func__, thread_count);
for (i = 0; i < thread_count ; i++) {
int ret;
if (!thread_array[i].active) {
Expand Down
28 changes: 23 additions & 5 deletions tools/nut-scanner/scan_snmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,11 @@ nutscan_device_t * nutscan_scan_snmp(const char * start_ip, const char * stop_ip
__func__);
max_threads_scantype = UINT_MAX - 1;
}
sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype);

upsdebugx(4, "%s: sem_init() for %" PRIuSIZE " threads", __func__, max_threads_scantype);
if (sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype)) {
upsdebug_with_errno(4, "%s: sem_init() failed", __func__);
}
}
# endif /* HAVE_SEMAPHORE */

Expand Down Expand Up @@ -1131,8 +1135,21 @@ nutscan_device_t * nutscan_scan_snmp(const char * start_ip, const char * stop_ip
sem_wait(semaphore);
pass = TRUE;
} else {
pass = ((max_threads_scantype == 0 || sem_trywait(semaphore_scantype) == 0) &&
sem_trywait(semaphore) == 0);
/* If successful (the lock was acquired),
* sem_wait() and sem_trywait() will return 0.
* Otherwise, -1 is returned and errno is set,
* and the state of the semaphore is unchanged.
*/
int stwST = sem_trywait(semaphore_scantype), stwS = sem_trywait(semaphore);
pass = ((max_threads_scantype == 0 || stwST == 0) && stwS == 0);
upsdebugx(4, "%s: max_threads_scantype=%" PRIuSIZE
" curr_threads=%" PRIuSIZE
" thread_count=%" PRIuSIZE
" stwST=%d stwS=%d pass=%d",
__func__, max_threads_scantype,
curr_threads, thread_count,
stwST, stwS, pass
);
}
# else
# ifdef HAVE_PTHREAD_TRYJOIN
Expand Down Expand Up @@ -1252,9 +1269,10 @@ nutscan_device_t * nutscan_scan_snmp(const char * start_ip, const char * stop_ip
# ifdef HAVE_SEMAPHORE
/* Wait for all current scans to complete */
if (thread_array != NULL) {
upsdebugx (2, "%s: Running too many scanning threads, "
upsdebugx (2, "%s: Running too many scanning threads (%"
PRIuSIZE "), "
"waiting until older ones would finish",
__func__);
__func__, thread_count);
for (i = 0; i < thread_count ; i++) {
int ret;
if (!thread_array[i].active) {
Expand Down
32 changes: 25 additions & 7 deletions tools/nut-scanner/scan_xml_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,11 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char
__func__);
max_threads_scantype = UINT_MAX - 1;
}
sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype);

upsdebugx(4, "%s: sem_init() for %" PRIuSIZE " threads", __func__, max_threads_scantype);
if (sem_init(semaphore_scantype, 0, (unsigned int)max_threads_scantype)) {
upsdebug_with_errno(4, "%s: sem_init() failed", __func__);
}
}
# endif /* HAVE_SEMAPHORE */

Expand All @@ -510,11 +514,24 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char
* all earlier runners */
if (max_threads_scantype > 0)
sem_wait(semaphore_scantype);
sem_wait(semaphore);
pass = TRUE;
sem_wait(semaphore);
pass = TRUE;
} else {
pass = ((max_threads_scantype == 0 || sem_trywait(semaphore_scantype) == 0) &&
sem_trywait(semaphore) == 0);
/* If successful (the lock was acquired),
* sem_wait() and sem_trywait() will return 0.
* Otherwise, -1 is returned and errno is set,
* and the state of the semaphore is unchanged.
*/
int stwST = sem_trywait(semaphore_scantype), stwS = sem_trywait(semaphore);
pass = ((max_threads_scantype == 0 || stwST == 0) && stwS == 0);
upsdebugx(4, "%s: max_threads_scantype=%" PRIuSIZE
" curr_threads=%" PRIuSIZE
" thread_count=%" PRIuSIZE
" stwST=%d stwS=%d pass=%d",
__func__, max_threads_scantype,
curr_threads, thread_count,
stwST, stwS, pass
);
}
# else
# ifdef HAVE_PTHREAD_TRYJOIN
Expand Down Expand Up @@ -643,9 +660,10 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char
# ifdef HAVE_SEMAPHORE
/* Wait for all current scans to complete */
if (thread_array != NULL) {
upsdebugx (2, "%s: Running too many scanning threads, "
upsdebugx (2, "%s: Running too many scanning threads (%"
PRIuSIZE "), "
"waiting until older ones would finish",
__func__);
__func__, thread_count);
for (i = 0; i < thread_count ; i++) {
int ret;
if (!thread_array[i].active) {
Expand Down

0 comments on commit d847727

Please sign in to comment.