~refi64/alys

740f7b74fee8344b9d0f64f82a51f8d1c3fd791d — Ryan Gonzalez 2 years ago 8ba935e
Encode a protocol version in .alys files
4 files changed, 45 insertions(+), 1 deletions(-)

M src/alys.cr
A src/alys/VERSION
M tools/alys_converter.cr
A tools/update_version.sh
M src/alys.cr => src/alys.cr +16 -1
@@ 116,6 116,13 @@ module Alys::Internal
    Free
  end

  PROTOCOL_VERSION = {{ read_file("#{__DIR__}/alys/VERSION").strip.id + "u32" }}

  @[Packed]
  record Header,
    name : UInt8[4],
    version : UInt32

  @[Packed]
  record Frame,
    ip : UInt64,


@@ 194,7 201,15 @@ module Alys::Internal

    Exception::CallStack.load_debug_info

    @@fd = EventlessFd.open file
    fd = EventlessFd.open file

    name = StaticArray['A', 'L', 'Y', 'S'].map { |c| c.bytes[0] }
    header = Header.new name: name, version: PROTOCOL_VERSION
    fd.write_value header

    # Don't save it until down here, since the previous lines would have started
    # allocations that would be written to this fd.
    @@fd = fd
  end

  @@in_write = false

A src/alys/VERSION => src/alys/VERSION +1 -0
@@ 0,0 1,1 @@
2022090801

M tools/alys_converter.cr => tools/alys_converter.cr +16 -0
@@ 30,12 30,20 @@ module ReadStructFromIO
  end
end

PROTOCOL_VERSION = Alys::Internal::PROTOCOL_VERSION

alias EventKind = Alys::Internal::EventKind
alias RawHeader = Alys::Internal::Header
alias RawEvent = Alys::Internal::Event
alias RawAllocEventExtra = Alys::Internal::AllocEventExtra
alias RawReallocEventExtra = Alys::Internal::ReallocEventExtra
alias RawFrame = Alys::Internal::Frame

@[Packed]
struct RawHeader
  include ReadStructFromIO
end

struct RawEvent
  include ReadStructFromIO
end


@@ 179,6 187,14 @@ class EventReader

    start_time = nil

    header = RawHeader.from_io @source
    puts header.name
    raise "Bad file format" if String.new(header.name.to_slice) != "ALYS"

    if header.version != PROTOCOL_VERSION
      raise "Incompatible protocol version #{header.version} (supported: #{PROTOCOL_VERSION})"
    end

    loop do
      begin
        raw_event = RawEvent.from_io @source

A tools/update_version.sh => tools/update_version.sh +12 -0
@@ 0,0 1,12 @@
#!/bin/bash

cd "$(dirname "$0")/.."

version_file=src/alys/VERSION
base_version=$(date -u +%F | tr -d '-')
[[ -f "$version_file" ]] && current_version=$(< "$version_file")
if [[ "$current_version" == "$base_version"* ]]; then
  echo "$(($current_version + 1))" > "$version_file"
else
  echo "${base_version}01" > "$version_file"
fi