stack.h 926 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * @file debug/stack.h
  3. * Stack usage analysis helpers
  4. */
  5. /*
  6. * Copyright (c) 2015 Intel Corporation
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. */
  10. #ifndef ZEPHYR_INCLUDE_DEBUG_STACK_H_
  11. #define ZEPHYR_INCLUDE_DEBUG_STACK_H_
  12. #include <logging/log.h>
  13. #include <stdbool.h>
  14. static inline void log_stack_usage(const struct k_thread *thread)
  15. {
  16. #if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO)
  17. size_t unused, size = thread->stack_info.size;
  18. LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
  19. if (k_thread_stack_space_get(thread, &unused) == 0) {
  20. unsigned int pcnt = ((size - unused) * 100U) / size;
  21. const char *tname;
  22. tname = k_thread_name_get((k_tid_t)thread);
  23. if (tname == NULL) {
  24. tname = "unknown";
  25. }
  26. LOG_INF("%p (%s):\tunused %zu\tusage %zu / %zu (%u %%)",
  27. thread, log_strdup(tname), unused, size - unused, size,
  28. pcnt);
  29. }
  30. #endif
  31. }
  32. #endif /* ZEPHYR_INCLUDE_DEBUG_STACK_H_ */