Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/MemLayoutGen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: MemLayout Info Generating
on:
pull_request:
jobs:
test:
runs-on: ${{ matrix.os }}
continue-on-error: true
strategy:
matrix:
version:
- '1.3.1'
- '1.4.2'
- '1.5.3'
- '1.5.4'
- '1.6.1'
os:
- ubuntu-latest
- windows-latest
- macOS-latest
arch:
- x64
- x86
exclude:
- os: macOS-latest
arch: x86
steps:
- name: Set up MinGW
uses: egor-tensin/setup-mingw@v2
with:
platform: i686
cc: 1
if: ${{ matrix.os == 'windows-latest' && matrix.arch == 'x86' }}
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- run: julia .github/workflows/tasklayout/memlayout.jl
- uses: EndBug/add-and-commit@v7
with:
add: 'src/memlayout'
author_name: GitHub Actions
2 changes: 1 addition & 1 deletion .github/workflows/Testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
test:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.version == 'nightly' }}
continue-on-error: true # ${{ matrix.version == 'nightly' }}
strategy:
matrix:
version:
Expand Down
138 changes: 138 additions & 0 deletions .github/workflows/tasklayout/memlayout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* linux64: g++ -I $JULIA_HOME/include/julia/ memlayout.cpp -o memlayout
* linux32: apt-get install g++-multilib
* g++ -march=pentium4 -m32 -I $JULIA_HOME/include/julia/ memlayout.cpp -o memlayout
* macOS: g++ -std=c++11 -I $JULIA_APP/Contents/Resources/julia/include/julia/ memlayout.cpp -o memlayout
* win64: g++ -I $JULIA_HOME/include/julia/ memlayout.cpp -o memlayout
* win32: g++ -I $JULIA_HOME/include/julia/ memlayout.cpp -o memlayout
**/

#include <string>
#include <map>
#include <fstream>
#include <sstream>

#include <cstddef>

#include "julia.h"

std::string jl_ver(std::string sep=".") {
std::ostringstream oss;
oss << JULIA_VERSION_MAJOR << sep
<< JULIA_VERSION_MINOR << sep
<< JULIA_VERSION_PATCH;
return oss.str();
}

std::string platform() {
#ifdef _WIN32
#ifdef _WIN64
return "windows-x86_64";
#endif
return "windows-x86";
#endif
#ifdef __APPLE__
return "darwin-x86_64";
#endif
#ifdef __linux__
#ifdef __x86_64
return "linux-x86_64";
#else
return "linux-x86";
#endif
#endif
}

std::map<std::string, size_t> task_field_offsets() {
std::map<std::string, size_t> data;

#define field_info(f) data[#f] = offsetof(jl_task_t, f)

field_info(next); // 131 142 153 154 160 170
field_info(queue); // 131 142 153 154 160 170
field_info(tls); // 131 142 153 154 160 170
field_info(donenotify); // 131 142 153 154 160 170
field_info(result); // 131 142 153 154 160 170

#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR < 6
field_info(state); // 131 142 153 154
field_info(exception); // 131 142 153 154
field_info(backtrace); // 131 142 153 154
#endif

field_info(logstate); // 131 142 153 154 160 170
field_info(start); // 131 142 153 154 160 170
field_info(sticky); // 131 142 153 154 160 170

#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR > 5
field_info(_state); // 160 170
field_info(_isexception); // 160 170
#endif

#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR > 6
field_info(rngState0); // 170
field_info(rngState1); // 170
field_info(rngState2); // 170
field_info(rngState3); // 170
#endif

// hidden state
field_info(gcstack); // 131 142 153 154 160 170

#if JULIA_VERSION_MAJOR == 1 && \
(JULIA_VERSION_MINOR > 6 || JULIA_VERSION_MINOR < 5 || \
(JULIA_VERSION_MINOR == 5 && JULIA_VERSION_PATCH < 4))
field_info(world_age); // 131 142 153 170
#endif

#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR > 6
field_info(ptls); // 170
#endif
field_info(tid); // 131 142 153 154 160 170
field_info(prio); // 131 142 153 154 160 170
field_info(excstack); // 131 142 153 154 160 170
field_info(eh); // 131 142 153 154 160 170
field_info(ctx); // 131 142 153 154 160 170

#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR < 6
field_info(locks); // 131 142 153 154
field_info(timing_stack); // 131 142 153 154
#endif
// field_info(copy_stack_ctx);

#if defined(JL_TSAN_ENABLED)
field_info(tsan_state); // 170
#endif

field_info(stkbuf); // 131 142 153 154 160 170
field_info(bufsz); // 131 142 153 154 160 170
// field_info(copy_stack);
// field_info(started);

#undef field_info
data["copy_stack"] = offsetof(jl_task_t, bufsz) + sizeof(size_t);
data["sizeof_ctx"] = sizeof(((jl_task_t*)0)->ctx);
// data["sizeof_stack_ctx"] = sizeof(((jl_task_t*)0)->copy_stack_ctx);
data["tls_base_context"] = offsetof(struct _jl_tls_states_t, base_ctx);
// data["tls_copy_stack_ctx"] = offsetof(struct _jl_tls_states_t, copy_stack_ctx);
return data;
}

int main() {
std::ofstream ofs(platform() +"-v" + jl_ver("_") + ".jl", std::ofstream::out);

ofs << "ALL_TASK_OFFSETS[("
<< '"' << platform() << '"' << ", "
<< "v\"" << jl_ver() <<"\""
<<")] = Dict(\n";
ofs << " :END => " << sizeof(jl_task_t) << ",\n";

std::map<std::string, size_t> data = task_field_offsets();
for(auto kv: data) {
ofs << " :" << kv.first << " => " << kv.second << ",\n";
}
ofs << ")\n";

ofs.close();
return 0;
}
32 changes: 32 additions & 0 deletions .github/workflows/tasklayout/memlayout.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const ARCH = @static if string(Sys.ARCH)[1] == 'i'
"x86"
else
string(Sys.ARCH)
end

const PLATFORM = @static if Sys.islinux()
"linux-" * ARCH
elseif Sys.iswindows()
"windows-" * ARCH
elseif Sys.isapple()
"darwin-" * ARCH
end

OUTPUT = "$(PLATFORM)-v$(VERSION.major)_$(VERSION.minor)_$(VERSION.patch).jl"
OUTPUT_DEST = joinpath("src/memlayout", OUTPUT)

isfile(OUTPUT_DEST) && exit(0)

const PROJECT_DIR = (@__DIR__) |> dirname |> dirname |> dirname
const INCLUDE = joinpath(dirname(Sys.BINDIR), "include/julia")
const OPTIONS = if string(Sys.ARCH)[1] == 'i'
Sys.islinux() && run(`sudo apt-get install g++-multilib -y`)
["-std=c++11","-march=pentium4", "-m32", "-static-libgcc", "-static-libstdc++"]
else
["-std=c++11"]
end

run(`g++ $(OPTIONS) -I$(INCLUDE) $(PROJECT_DIR)/.github/workflows/tasklayout/memlayout.cpp -o memlayout-gen.exe`)
run(`./memlayout-gen.exe`)

isfile(OUTPUT) && Base.Filesystem.mv(OUTPUT, OUTPUT_DEST)
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ repo = "https://github.com/TuringLang/Libtask.jl.git"
version = "0.5.3"

[deps]
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Libtask_jll = "3ae2931a-708c-5973-9c38-ccf7496fb450"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
Libtask_jll = "0.4"
Libtask_jll = "0.5.1"
julia = "1.3"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "BenchmarkTools"]
10 changes: 10 additions & 0 deletions src/Libtask.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
module Libtask

using Libdl
using Libtask_jll

export CTask, consume, produce, TArray, tzeros, tfill, TRef

function __init__()
@static if VERSION < v"1.6.0"
push!(Libdl.DL_LOAD_PATH,
Libtask_jll.get_libtask_julia_path() |> dirname)
Libdl.dlopen(Libtask_jll.libtask_julia_path)
end
end

include("memlayout/main.jl")
include("ctask.jl")
include("tarray.jl")
include("tref.jl")
Expand Down
38 changes: 36 additions & 2 deletions src/ctask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ function enable_stack_copying(t::Task)
if istaskfailed(t)
error("only runnable or finished tasks' stack can be copied.")
end
return ccall((:jl_enable_stack_copying, libtask_julia), Any, (Any,), t)::Task
# return ccall((:jl_enable_stack_copying, libtask_julia),
# Any, (Any,), t)::Task
copy_stack = internal_getfield(t, :copy_stack, Int32)
if copy_stack == 0
internal_setfield(t, :copy_stack, Int32(1))
internal_setfield(t, :bufsz, Csize_t(0))
ccall((:jl_reset_task_ctx, libtask_julia),
Cvoid, (Any, Csize_t, Csize_t, Csize_t),
t, TASK_OFFSETS[:ctx], TASK_OFFSETS[:sizeof_ctx],
TASK_OFFSETS[:tls_base_context])
end
return t
end

"""
Expand Down Expand Up @@ -101,7 +112,30 @@ function Base.copy(ctask::CTask)
error("only runnable or finished tasks can be copied.")
end

newtask = ccall((:jl_clone_task, libtask_julia), Any, (Any,), task)::Task
# memory copy
# newtask = ccall((:jl_clone_task, libtask_julia), Any, (Any,), task)::Task
newtask = ccall((:jl_clone_task_opaque, libtask_julia),
Any, (Any, Csize_t), task, TASK_OFFSETS[:END])::Task
internal_setfield(newtask, :exception, nothing)
internal_setfield(newtask, :backtrace, nothing)
internal_setfield(newtask, :tls, nothing)
internal_setfield(newtask, :result, nothing)
internal_setfield(newtask, :donenotify, nothing)
internal_setfield(newtask, :excstack, C_NULL)
internal_setfield(newtask, :ptls, C_NULL)
internal_setfield(newtask, :gcstack, C_NULL)

if haskey(TASK_OFFSETS, :stkbuf) && haskey(TASK_OFFSETS, :bufsz)
old_stkbuf = internal_getfield(task, :stkbuf, Ptr)
if old_stkbuf != C_NULL
internal_setfield(task, :bufsz, Csize_t(0))
else
internal_setfield(newtask, :stkbuf, C_NULL)
end
internal_setfield(newtask, :bufsz, Csize_t(0))
end
memset(newtask, 0, :locks)
# memory copy done

task.storage[:n_copies] = 1 + n_copies(task)
newtask.storage = copy(task.storage)
Expand Down
28 changes: 28 additions & 0 deletions src/memlayout/darwin-x86_64-v1_3_1.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ALL_TASK_OFFSETS[("darwin-x86_64", v"1.3.1")] = Dict(
:END => 568,
:backtrace => 56,
:bufsz => 248,
:copy_stack => 256,
:ctx => 84,
:donenotify => 32,
:eh => 264,
:exception => 48,
:excstack => 280,
:gcstack => 272,
:locks => 304,
:logstate => 64,
:next => 0,
:prio => 298,
:queue => 8,
:result => 40,
:sizeof_ctx => 152,
:start => 72,
:state => 24,
:sticky => 80,
:stkbuf => 240,
:tid => 296,
:timing_stack => 560,
:tls => 16,
:tls_base_context => 6664,
:world_age => 288,
)
28 changes: 28 additions & 0 deletions src/memlayout/darwin-x86_64-v1_4_2.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ALL_TASK_OFFSETS[("darwin-x86_64", v"1.4.2")] = Dict(
:END => 568,
:backtrace => 56,
:bufsz => 248,
:copy_stack => 256,
:ctx => 84,
:donenotify => 32,
:eh => 264,
:exception => 48,
:excstack => 280,
:gcstack => 272,
:locks => 304,
:logstate => 64,
:next => 0,
:prio => 298,
:queue => 8,
:result => 40,
:sizeof_ctx => 152,
:start => 72,
:state => 24,
:sticky => 80,
:stkbuf => 240,
:tid => 296,
:timing_stack => 560,
:tls => 16,
:tls_base_context => 6664,
:world_age => 288,
)
Loading