Skip to content

Comments

std.experimental.logger: allow precise scanning of DATA/TLS segment#5357

Merged
dlang-bot merged 2 commits intodlang:masterfrom
rainers:logger_prealloc
May 2, 2017
Merged

std.experimental.logger: allow precise scanning of DATA/TLS segment#5357
dlang-bot merged 2 commits intodlang:masterfrom
rainers:logger_prealloc

Conversation

@rainers
Copy link
Member

@rainers rainers commented May 2, 2017

Buffers for preallocated class instances should be typed void[], not ubyte[] and must be properly aligned.

This blocks dlang/druntime#1798.

…class instances should be typed void[], not ubyte[] and must be properly aligned
@rainers rainers force-pushed the logger_prealloc branch from 9402b32 to 8803c5e Compare May 2, 2017 12:16
Copy link
Member

@burner burner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

import std.stdio : stderr;

static __gshared ubyte[__traits(classInstanceSize, FileLogger)] _buffer;
static __gshared align(FileLogger.alignof) void[__traits(classInstanceSize, FileLogger)] _buffer;
Copy link
Member

@PetarKirov PetarKirov May 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andralex druntime & phobos are full of such ad hoc implementations of the singleton pattern (the last being 5355) we should really try to get singleton!T merged and fix this once and for all. Do you want me to take over the singleton implementation from dlang/druntime#1710?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZombineDev that'd be great, thanks. There seem to be three kinds - thread-local, shared, and gshared.

Copy link
Member

@PetarKirov PetarKirov May 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine they would work like so:

// at module level
Singleton!T instance; // thread-local
shared Singleton!T shared_instance; // shared
__gshared Singleton!T gshared_instance; // __gshared

pragma (msg, typeof(instance.get()); // T
pragma (msg, typeof(shared_instance.get()); // shared(T)
pragma (msg, typeof(gshared_instance.get()); // T

// at function/aggregate scope
/* static is implicit in the implementation*/ Singleton!T instance; // thread-local
/* static is implicit in the implementation */ shared Singleton!T shared_instance; // shared
__gshared Singleton!T gshared_instance; // __gshared

pragma (msg, typeof(instance.get()); // T
pragma (msg, typeof(shared_instance.get()); // shared(T)
pragma (msg, typeof(gshared_instance.get()); // T

I.e. overload Singleton(T)'s methods on shared-ness.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZombineDev the singleton wouldn't appear at module level - that's a global. It needs to be a function returning a reference.

import std.conv : emplace;

static ubyte[__traits(classInstanceSize, StdForwardLogger)] _buffer;
static /*align(StdForwardLogger.alignof)*/ void[__traits(classInstanceSize, StdForwardLogger)] _buffer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If setting alignment explicitly doesn't work, maybe the following would:
size_t[1 + (__traits(classInstanceSize, StdForwardLogger) - 1) / size_t.sizeof] _buffer

Copy link
Member Author

@rainers rainers May 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could work with size_t replaced by void*. It get's pretty verbose, though. It also doesn't support possible larger alignment requirements, but these should not be required here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's divideRoundUp in std.experimental.allocator, perhaps it should be moved up.

Copy link
Member

@PetarKirov PetarKirov May 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could work with size_t replaced by void*

At first I thought about proposing void* but then I chose size_t for simplicity. But shouldn't all integral types N have N.alignof == N.sizeof?

It get's pretty verbose, though.

Agreed, that's why I'm proposing to abstract those details in a singleton(T) template ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amended a respective change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I thought about proposing void* but then I chose size_t for simplicity. But shouldn't all integral types N have N.alignof == N.sizeof?

Yes, but using size_t would avoid scanning that area as ubyte[] does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I missed the obvious :D

@rainers
Copy link
Member Author

rainers commented May 2, 2017

Unfortunately, ld for x64 doesn't seem to like the alignment specification for TLS data. I have commented these.

@rainers rainers force-pushed the logger_prealloc branch from 54dc219 to a3e7298 Compare May 2, 2017 13:27
@rainers
Copy link
Member Author

rainers commented May 2, 2017

druntime & phobos are full of such ad hoc implementations of the singleton behavior (the last being 5355) we should really try to get singleton!T merged and fix this once and for all.

I grepped for similar code and found a few, but AFAICT none worked on classes with pointers (the monitor pointer is manually managed, so doesn't need to be marked for precise scanning). It would be nice if a generic version could cover all instances.

@PetarKirov
Copy link
Member

PetarKirov commented May 2, 2017

@rainers feel free to merge now that everything is green.

@dlang-bot dlang-bot merged commit 78acf07 into dlang:master May 2, 2017
@rainers
Copy link
Member Author

rainers commented May 2, 2017

Thanks for fast review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants