Skip to content
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

rework std.Progress #20059

Merged
merged 60 commits into from
May 28, 2024
Merged

rework std.Progress #20059

merged 60 commits into from
May 28, 2024

Commits on May 28, 2024

  1. start reworking std.Progress

    New design ideas:
    * One global instance, don't try to play nicely with other instances
      except via IPC.
    * One process owns the terminal and the other processes communicate via
      IPC.
    * Clear the whole terminal and use multiple lines.
    
    What's implemented so far:
    * Query the terminal for size.
    * Register a SIGWINCH handler.
    * Use a thread for redraws.
    
    To be done:
    * IPC
    * Handling single threaded targets
    * Porting to Windows
    * More intelligent display of the progress tree rather than only using
      one line.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    d6e8ba3 View commit details
    Browse the repository at this point in the history
  2. progress progress

    Move the mutex into the nodes
    
    Track the whole tree instead of only recently activated node
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    e1e4de2 View commit details
    Browse the repository at this point in the history
  3. rework std.Progress again

    This time, we preallocate a fixed set of nodes and have the user-visible
    Node only be an index into them. This allows for lock-free management of
    the node storage.
    
    Only the parent indexes are stored, and the update thread makes a
    serialized copy of the state before trying to compute children lists.
    
    The update thread then walks the tree and outputs an entire tree of
    progress rather than only one line.
    
    There is a problem with clearing from the cursor to the end of the
    screen when the cursor is at the bottom of the terminal.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    a3c9511 View commit details
    Browse the repository at this point in the history
  4. fix terminal repainting

    the clear, save, restore thing doesn't work when the terminal is at the
    bottom
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    66c3b6a View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    582acdf View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    67e08e7 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    ed36470 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    f071164 View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    df46f5a View commit details
    Browse the repository at this point in the history
  10. std.Progress: save a copy of IPC data

    so that the previous message can be used when the pipe is empty.
    
    prevents flickering
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    3a768bd View commit details
    Browse the repository at this point in the history
  11. std.process.Child: fix ZIG_PROGRESS env var handling

    and properly dup2 the file descriptor to make it handle the case when
    other files are already open
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    c01cfde View commit details
    Browse the repository at this point in the history
  12. std.Progress: fix using saved IPC data

    also fix handling of BrokenPipe
    
    also fix continuing wrong loop in error conditions
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    f6873c6 View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    f97c2f2 View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    795c579 View commit details
    Browse the repository at this point in the history
  15. Configuration menu
    Copy the full SHA
    a486392 View commit details
    Browse the repository at this point in the history
  16. Configuration menu
    Copy the full SHA
    1cf1cb6 View commit details
    Browse the repository at this point in the history
  17. Configuration menu
    Copy the full SHA
    b27fdf9 View commit details
    Browse the repository at this point in the history
  18. Configuration menu
    Copy the full SHA
    2233d95 View commit details
    Browse the repository at this point in the history
  19. std.Progress: fixes

    * bump default statically allocated resources
    * debug help when multiple instances of std.Progress are initialized
    * only handle sigwinch on supported operating systems
    * handle when reading from the pipe returns 0 bytes
    * avoid printing more lines than rows
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    70e39c1 View commit details
    Browse the repository at this point in the history
  20. Configuration menu
    Copy the full SHA
    352dc2c View commit details
    Browse the repository at this point in the history
  21. Configuration menu
    Copy the full SHA
    2759173 View commit details
    Browse the repository at this point in the history
  22. Configuration menu
    Copy the full SHA
    516366f View commit details
    Browse the repository at this point in the history
  23. std.Progress: fix race condition with IPC nodes

    It stored some metadata into the canonical node storage data but that is
    a race condition because another thread recycles those nodes.
    
    Also, keep the parent name for empty child root node names.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    ca03c9c View commit details
    Browse the repository at this point in the history
  24. Configuration menu
    Copy the full SHA
    9331da8 View commit details
    Browse the repository at this point in the history
  25. std.process.Child: prevent racing children from inheriting progress p…

    …ipes
    
    This fix is already in master branch for stdin, stdout, and stderr; this
    commit solves the same problem but for the progress pipe.
    
    Both fixes were originally included in one commit on this branch,
    however it was split it into two so that master branch could receive the
    fix before the progress branch is merged.
    mlugg authored and andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    acdf988 View commit details
    Browse the repository at this point in the history
  26. Module: fix and improve progress reporting

    * correctly report time spent analyzing function bodies
    * print fully qualified decl names
    * also have a progress node for codegen
    
    The downside of these changes is that it's a bit flickerey, but the
    upside is that it's accurate; you can see what the compiler's doing!
    mlugg authored and andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    d403d8c View commit details
    Browse the repository at this point in the history
  27. Progress: fix compile errors on windows

    Works for `zig build-exe`, IPC still not implemented yet.
    jacobly0 authored and andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    d77f5e7 View commit details
    Browse the repository at this point in the history
  28. Configuration menu
    Copy the full SHA
    e8907f9 View commit details
    Browse the repository at this point in the history
  29. std.process.Child: comptime assert to protect prog_fileno

    documenting my assumptions via comptime assertion
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    e2e61f3 View commit details
    Browse the repository at this point in the history
  30. Configuration menu
    Copy the full SHA
    e820173 View commit details
    Browse the repository at this point in the history
  31. Configuration menu
    Copy the full SHA
    eb718ce View commit details
    Browse the repository at this point in the history
  32. frontend: fix use of undefined progress node

    This was causing a crash when running `zig test`.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    d3b03ed View commit details
    Browse the repository at this point in the history
  33. std.Progress: move global preallocations to thread memory

    Instead of making static buffers configurable, let's pick strong
    defaults and then use the update thread's stack memory to store the
    preallocations. The thread uses a fairly shallow stack so this memory is
    otherwise unused. This also makes the data section of the executable
    smaller since it runtime allocates the memory when a `std.Progress`
    instance is allocated, and in the case that the process is not connected
    to a terminal, it never allocates the memory.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    7fe72d5 View commit details
    Browse the repository at this point in the history
  34. std.Progress: move more global preallocations to thread memory

    Same idea as previous commit
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    807b613 View commit details
    Browse the repository at this point in the history
  35. std.Progress: truncate IPC data exceeding preallocated buffers

    This accomplishes 2 things simultaneously:
    
    1. Don't trust child process data; if the data is outside the expected
       range, ignore the data.
    2. If there is too much data to fit in the preallocated buffers, drop
       the data.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    52ed54d View commit details
    Browse the repository at this point in the history
  36. Configuration menu
    Copy the full SHA
    11f8947 View commit details
    Browse the repository at this point in the history
  37. std.Progress: smaller type for parents and robustify

    Switch Node.Parent, Node.Index, and Node.OptionalIndex to be backed by
    u8 rather than u16. This works fine since we use 200 as the preallocated
    node buffer. This has the nice property that scanning the entire parents
    array for allocated nodes fits in 4 cache lines, even if we bumped the
    200 up to 254 (leaving room for the two special states).
    
    The thread that reads progress updates from the pipe now handles short
    reads by ignoring messages that are sent in multiple reads.
    
    When checking the terminal size, if there is a failure, fall back to a
    conservative guess of 80x25 rather than panicking. A debug message is
    also emitted which would be displayed only in a debug build.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    ea7d8ec View commit details
    Browse the repository at this point in the history
  38. std.Progress: use std.log.debug rather than warn

    when the errors could possibly be spammed many times
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    0ca2b4e View commit details
    Browse the repository at this point in the history
  39. zig build: give a root progress name

    Now it's more clear when zig is building the build script vs building
    the actual project.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    849693f View commit details
    Browse the repository at this point in the history
  40. std.Progress: keep cursor on newline

    Don't truncate trailing newline. This better handles stray writes to
    stderr that are not std.Progress-aware, such as from non-zig child
    processes.
    
    This commit also makes `Node.start` and `Node.end` bail out early with a
    comptime branch when it is known the target will not be spawning an
    update thread.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    52ffdec View commit details
    Browse the repository at this point in the history
  41. Configuration menu
    Copy the full SHA
    6145819 View commit details
    Browse the repository at this point in the history
  42. std.Progress: count newlines more accurately

    Split newline_count into written_newline_count and
    accumulated_newline_count. This handle the case when the tryLock() fails
    to obtain the lock, because in such case there would not be any newlines
    written to the terminal but the system would incorrectly think there
    were. Now, written_newline_count is only adjusted when the write() call
    succeeds.
    
    Furthermore, write() call failure is handled by exiting the update
    thread.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    dc3a192 View commit details
    Browse the repository at this point in the history
  43. std.Progress: elide root node if empty

    when the root progress node has a zero length name, the sub-tree is
    flattened one layer, reducing visual noise, as well as bytes written to
    the terminal.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    64c6a50 View commit details
    Browse the repository at this point in the history
  44. CLI: add missing call to root progress node end()

    cleans up unwanted "LLVM Emit Object" being left on the screen
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    c9587d3 View commit details
    Browse the repository at this point in the history
  45. std.process.cleanExit: lock stderr before exiting

    This makes it so that any other threads which are writing to stderr have
    a chance to finish before the process terminates. It also clears the
    terminal in case any progress has been written to stderr, while still
    accomplishing the goal of not waiting until the update thread exits.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    45b505c View commit details
    Browse the repository at this point in the history
  46. Configuration menu
    Copy the full SHA
    03073d6 View commit details
    Browse the repository at this point in the history
  47. Configuration menu
    Copy the full SHA
    0937992 View commit details
    Browse the repository at this point in the history
  48. std.Build.Step.Run: account for new environment variable

    Introduces `disable_zig_progress` which prevents the build runner from
    assigning the child process a progress node.
    
    This is needed for the empty_env test which requires the environment to
    be completely empty.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    a5e4fe5 View commit details
    Browse the repository at this point in the history
  49. std.Progress: adjust the timings a little bit

    Slightly slower refresh rate. It's still updating very quickly.
    
    Lower the initial delay so that CLI applications feel more responsive.
    Even though the application is doing work for the full 500ms until
    something is displayed on the screen, it feels nicer to get the progress
    earlier.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    eea7e5e View commit details
    Browse the repository at this point in the history
  50. std.Progress: handle big-endian targets

    We cannot rely on host endianness because the parent or child process
    may be executing inside QEMU.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    dcf9cae View commit details
    Browse the repository at this point in the history
  51. Configuration menu
    Copy the full SHA
    2367a1f View commit details
    Browse the repository at this point in the history
  52. Configuration menu
    Copy the full SHA
    4438925 View commit details
    Browse the repository at this point in the history
  53. Configuration menu
    Copy the full SHA
    bb1f4d2 View commit details
    Browse the repository at this point in the history
  54. std.Progress: fix race condition with setIpcFd

    The update thread was sometimes reading the special state and then
    incorrectly getting 0 for the file descriptor, making it hang since it
    tried to read from stdin.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    aca7feb View commit details
    Browse the repository at this point in the history
  55. zig build: respect --color argument

    `--color off` now disables the CLI progress bar both in the parent
    process and the build runner process.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    b7889f2 View commit details
    Browse the repository at this point in the history
  56. std.process.Child: fix spawning child proc with new cwd fd

    Before this fix, the dup2 of the progress pipe was clobbering the cwd
    fd, causing the fchdir to return ENOTDIR in between fork() and exec().
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    947a3a1 View commit details
    Browse the repository at this point in the history
  57. CLI: actually fix translate-c creating root progress node twice

    7281cc1 did not solve the problem
    because even when Node.index is none, it still counts as initializing
    the global Progress object. Just use a normal zig optional, and all is
    good.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    5bdfe22 View commit details
    Browse the repository at this point in the history
  58. std.Progress: relax some of the atomic orderings

    Generates better machine code, particularly on ARM
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    65a0e14 View commit details
    Browse the repository at this point in the history
  59. std.Progress: Use Windows console API calls when ANSI escape codes ar…

    …e not supported
    squeek502 authored and andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    40afac4 View commit details
    Browse the repository at this point in the history
  60. std.Progress: better Windows support

    * Merge a bunch of related state together into TerminalMode. Windows
      sometimes follows the same path as posix via ansi_escape_codes,
      sometimes not.
    * Use a different thread entry point for Windows API but share the same
      entry point on Windows when the terminal is in ansi_escape_codes mode.
    * Only clear the terminal when the stderr lock is held.
    * Don't try to clear the terminal when nothing has been written yet.
    * Don't try to clear the terminal in IPC mode.
    * Fix size detection logic bug under error conditions.
    andrewrk committed May 28, 2024
    Configuration menu
    Copy the full SHA
    3a3d218 View commit details
    Browse the repository at this point in the history