-
Notifications
You must be signed in to change notification settings - Fork 88
Fix: emit each sim device-log record atomically (#1697) #1699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ChaoZheng109
merged 2 commits into
hw-native-sys:main
from
ChaoZheng109:fix/issue-1697-sim-device-log-atomic-record
Aug 6, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| /* | ||
| * Copyright (c) PyPTO Contributors. | ||
| * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See LICENSE in the root of the software repository for the full text of the License. | ||
| * ----------------------------------------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| // Sim device-log atomicity: every dev_vlog_* call emits exactly one intact | ||
| // physical line, even when many AICPU sim threads or forked chip workers write | ||
| // the shared stderr concurrently. Each record is <= PIPE_BUF, so a single | ||
| // write(2) to a pipe is atomic and cannot interleave with another writer. | ||
|
|
||
| #include <cstdarg> | ||
| #include <cstdio> | ||
| #include <set> | ||
| #include <string> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| #include <sys/wait.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "aicpu/device_log.h" | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr const char *kTags[] = {"DEBUG", "INFO", "TIMING", "WARN", "ERROR"}; | ||
|
|
||
| // dev_vlog_* gate on nothing (the unified_log_* adapter owns level filtering), | ||
| // so these thin wrappers always emit. level_idx selects the backend under test. | ||
| void emit(int level_idx, const char *func, const char *fmt, ...) { | ||
| va_list ap; | ||
| va_start(ap, fmt); | ||
| switch (level_idx % 5) { | ||
| case 0: | ||
| dev_vlog_debug(func, fmt, ap); | ||
| break; | ||
| case 1: | ||
| dev_vlog_info(func, fmt, ap); | ||
| break; | ||
| case 2: | ||
| dev_vlog_timing(func, fmt, ap); | ||
| break; | ||
| case 3: | ||
| dev_vlog_warn(func, fmt, ap); | ||
| break; | ||
| default: | ||
| dev_vlog_error(func, fmt, ap); | ||
| break; | ||
| } | ||
| va_end(ap); | ||
| } | ||
|
|
||
| std::string record(int level_idx, const char *func, const std::string &body) { | ||
| return std::string("[") + kTags[level_idx % 5] + "] " + func + ": " + body; | ||
| } | ||
|
|
||
| // Redirect stderr onto a fresh pipe. All writers stay well under the pipe's | ||
| // 64 KiB capacity, so they never block before the reader drains. | ||
| struct Capture { | ||
| int read_fd = -1; | ||
| int saved_stderr = -1; | ||
| }; | ||
|
|
||
| Capture begin_capture() { | ||
| int fds[2]; | ||
| EXPECT_EQ(pipe(fds), 0); | ||
| fflush(stderr); | ||
| Capture cap; | ||
| cap.saved_stderr = dup(STDERR_FILENO); | ||
| EXPECT_GE(cap.saved_stderr, 0); | ||
| EXPECT_GE(dup2(fds[1], STDERR_FILENO), 0); | ||
| close(fds[1]); // STDERR_FILENO is now the sole write handle | ||
| cap.read_fd = fds[0]; | ||
| return cap; | ||
| } | ||
|
|
||
| // Restore stderr (closing the last write handle so read hits EOF) and slurp | ||
| // everything the pipe holds. | ||
| std::string end_capture(Capture &cap) { | ||
| fflush(stderr); | ||
| EXPECT_GE(dup2(cap.saved_stderr, STDERR_FILENO), 0); | ||
| close(cap.saved_stderr); | ||
|
|
||
| std::string out; | ||
| char buf[4096]; | ||
| ssize_t n; | ||
| while ((n = read(cap.read_fd, buf, sizeof(buf))) > 0) { | ||
| out.append(buf, static_cast<size_t>(n)); | ||
| } | ||
| close(cap.read_fd); | ||
| return out; | ||
| } | ||
|
|
||
| // Assert the captured stream is exactly `expected`, one intact record per | ||
| // physical line — a torn (merged or split) record matches no expected string. | ||
| void expect_intact(const std::string &captured, std::multiset<std::string> expected) { | ||
| size_t start = 0; | ||
| while (start < captured.size()) { | ||
| size_t nl = captured.find('\n', start); | ||
| ASSERT_NE(nl, std::string::npos) << "record missing terminating newline"; | ||
| std::string line = captured.substr(start, nl - start); | ||
| auto it = expected.find(line); | ||
| ASSERT_NE(it, expected.end()) << "torn or unexpected record: '" << line << "'"; | ||
| expected.erase(it); | ||
| start = nl + 1; | ||
| } | ||
| EXPECT_TRUE(expected.empty()) << expected.size() << " records never arrived intact"; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(SimDeviceLogTest, MultiThreadedRecordsStayIntact) { | ||
| constexpr int kThreads = 4; | ||
| constexpr int kPerThread = 200; | ||
|
|
||
| std::multiset<std::string> expected; | ||
| for (int t = 0; t < kThreads; ++t) { | ||
| for (int i = 0; i < kPerThread; ++i) { | ||
| char body[32]; | ||
| snprintf(body, sizeof(body), "t%d-r%04d", t, i); | ||
| expected.insert(record(t, "worker", body)); | ||
| } | ||
| } | ||
|
|
||
| Capture cap = begin_capture(); | ||
| std::vector<std::thread> threads; | ||
| threads.reserve(kThreads); | ||
| for (int t = 0; t < kThreads; ++t) { | ||
| threads.emplace_back([t] { | ||
| for (int i = 0; i < kPerThread; ++i) { | ||
| emit(t, "worker", "t%d-r%04d", t, i); | ||
| } | ||
| }); | ||
| } | ||
| for (auto &th : threads) { | ||
| th.join(); | ||
| } | ||
| std::string captured = end_capture(cap); | ||
|
|
||
| ASSERT_NO_FATAL_FAILURE(expect_intact(captured, std::move(expected))); | ||
| } | ||
|
|
||
| TEST(SimDeviceLogTest, ForkedProcessesEmitWholeRecords) { | ||
| constexpr int kChildren = 8; | ||
| constexpr int kPerChild = 100; | ||
|
|
||
| std::multiset<std::string> expected; | ||
| for (int c = 0; c < kChildren; ++c) { | ||
| for (int i = 0; i < kPerChild; ++i) { | ||
| char body[32]; | ||
| snprintf(body, sizeof(body), "c%d-r%03d", c, i); | ||
| expected.insert(record(c, "chip_worker", body)); | ||
| } | ||
| } | ||
|
|
||
| Capture cap = begin_capture(); | ||
| std::vector<pid_t> pids; | ||
| pids.reserve(kChildren); | ||
| for (int c = 0; c < kChildren; ++c) { | ||
| pid_t pid = fork(); | ||
| ASSERT_GE(pid, 0); | ||
| if (pid == 0) { | ||
| for (int i = 0; i < kPerChild; ++i) { | ||
| emit(c, "chip_worker", "c%d-r%03d", c, i); | ||
| } | ||
| _exit(0); // skip gtest/atexit teardown so nothing else hits the pipe | ||
| } | ||
| pids.push_back(pid); | ||
| } | ||
| for (pid_t pid : pids) { | ||
| int status = 0; | ||
| ASSERT_EQ(waitpid(pid, &status, 0), pid); | ||
| ASSERT_TRUE(WIFEXITED(status)) << "child terminated abnormally"; | ||
| EXPECT_EQ(WEXITSTATUS(status), 0); | ||
| } | ||
| std::string captured = end_capture(cap); | ||
|
|
||
| ASSERT_NO_FATAL_FAILURE(expect_intact(captured, std::move(expected))); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Update the stale backend description in the file layout block.
The new text at Lines 142-147 is accurate. Line 64 of the same file still describes the sim backend as
sim backend (fprintf to stderr). That description is now wrong, and the PR objective asks for the inaccurate sim logging documentation to be corrected.📝 Proposed doc fix (Line 64)
🤖 Prompt for AI Agents