|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#if defined(XTENSA) |
| 10 | + |
| 11 | +#include <stdio.h> |
| 12 | +#include <sys/times.h> |
| 13 | + |
| 14 | +#include <xtensa/sim.h> |
| 15 | + |
| 16 | +#include <executorch/runtime/platform/platform.h> |
| 17 | + |
| 18 | +#define ET_LOG_OUTPUT_FILE stdout |
| 19 | + |
| 20 | +void et_pal_emit_log_message( |
| 21 | + et_timestamp_t timestamp, |
| 22 | + et_pal_log_level_t level, |
| 23 | + const char* filename, |
| 24 | + ET_UNUSED const char* function, |
| 25 | + size_t line, |
| 26 | + const char* message, |
| 27 | + ET_UNUSED size_t length) { |
| 28 | + // Not all platforms have ticks == nanoseconds, but this one does. |
| 29 | + timestamp /= 1000; // To microseconds |
| 30 | + int us = timestamp % 1000000; |
| 31 | + timestamp /= 1000000; // To seconds |
| 32 | + int sec = timestamp % 60; |
| 33 | + timestamp /= 60; // To minutes |
| 34 | + int min = timestamp % 60; |
| 35 | + timestamp /= 60; // To hours |
| 36 | + int hour = timestamp; |
| 37 | + |
| 38 | + fprintf( |
| 39 | + ET_LOG_OUTPUT_FILE, |
| 40 | + "%c %02d:%02d:%02d.%06d executorch:%s:%d] %s\n", |
| 41 | + static_cast<char>(level), |
| 42 | + hour, |
| 43 | + min, |
| 44 | + sec, |
| 45 | + us, |
| 46 | + filename, |
| 47 | + static_cast<int>(line), |
| 48 | + message); |
| 49 | + fflush(ET_LOG_OUTPUT_FILE); |
| 50 | +} |
| 51 | + |
| 52 | +et_timestamp_t et_pal_current_ticks(void) { |
| 53 | + struct tms curr_time; |
| 54 | + times(&curr_time); |
| 55 | + return curr_time.tms_utime; |
| 56 | +} |
| 57 | + |
| 58 | +void et_pal_init(void) { |
| 59 | + xt_iss_client_command("all", "enable"); |
| 60 | +} |
| 61 | + |
| 62 | +#else |
| 63 | + |
| 64 | +#include <time.h> |
| 65 | + |
| 66 | +#include <cstdio> |
| 67 | +#include <cstdlib> |
| 68 | + |
| 69 | +#include <executorch/runtime/platform/platform.h> |
| 70 | + |
| 71 | +#define ET_LOG_OUTPUT_FILE stderr |
| 72 | + |
| 73 | +#define NSEC_PER_USEC 1000UL |
| 74 | +#define USEC_IN_SEC 1000000UL |
| 75 | +#define NSEC_IN_USEC 1000UL |
| 76 | +#define NSEC_IN_SEC (NSEC_IN_USEC * USEC_IN_SEC) |
| 77 | + |
| 78 | +et_timestamp_t et_pal_current_ticks(void) { |
| 79 | + struct timespec ts; |
| 80 | + auto ret = clock_gettime(CLOCK_REALTIME, &ts); |
| 81 | + if (ret != 0) { |
| 82 | + fprintf(ET_LOG_OUTPUT_FILE, "Could not get time\n"); |
| 83 | + fflush(ET_LOG_OUTPUT_FILE); |
| 84 | + std::abort(); |
| 85 | + } |
| 86 | + |
| 87 | + return ((ts.tv_sec * NSEC_IN_SEC) + (ts.tv_nsec)); |
| 88 | +} |
| 89 | + |
| 90 | +#endif |
0 commit comments