Accessing x86 Performance Counters
I was wondering if anyone has written D code to access the x86 performance counters, to get information such as the number of cache misses and cycle count.
Re: Explicitly Freeing Memory
Unfortunately I don't have any good suggestions... I have been avoiding depending on dtors in D because of the aforementioned issues (and more), so I haven't had much experience in debugging dtor-related problems in D. I decided to just free everything explicitly: https://github.com/maximecb/Higgs/blob/03931178deb5794bf27b1020542d102a08286c07/source/runtime/vm.d#L784 It seems to address my memory leak issue for the moment: `make test` uses about 1/3 the memory and runs more than twice as fast.
Explicitly Freeing Memory
I posted a thread the other day explaining that I was running into a memory leak issue which is very hard to debug. There seems to be a false pointer somewhere, and I have no way of knowing where that is or which object is being pointed to. I decided to take the easy way out and explicitly free memory when I don't need it. Unfortunately, this brings about more problems. I'm trying to explicitly free chunks of memory allocated with GC.malloc() in a destructor. This works fine while the program is running, but when the program terminates, it seems the GC calls all destructors in an arbitrary order. I then get a core.exception.InvalidMemoryOperationError because I'm trying to free memory that is already freed. Not quite sure where to go from here... I guess I'd have to allocate all of the objects I want to free outside of GC'd space, but D doesn't seem to provide a convenient syntax for doing so. The alternate solution I'm considering is to have a special function to destroy all the objects owned by my parent object before destroying the parent object itself, and calling this function explicitly.
Debugging a Memory Leak
There seems to be a memory leak in the Higgs compiler. This problem shows up when running our test suite (`make test` command). A new VM object is created for each unittest block, e.g.: https://github.com/maximecb/Higgs/blob/master/source/runtime/tests.d#L201 These VM objects are unfortunately *never freed*. Not until the whole series of tests is run and the process terminates. The VM objects keep references to many other objects, and so the process keeps using more and more memory, up to over 2GB. The VM allocates it's own JS data heap that it manages itself, i.e.: https://github.com/maximecb/Higgs/blob/master/source/runtime/gc.d#L186 This memory is clearly marked as NO_SCAN, and so references to the VM in there should presumably not be counted. There is also executable memory I allocate with mmap, but this should also be ignored by the D GC in principle (I do not mark executable code as roots): https://github.com/maximecb/Higgs/blob/master/source/jit/codeblock.d#L129 I don't know where the problem lies. There could be false pointers, but I'm on a 64-bit system, which should presumably make this less likely. I wish there was a way to ask the D runtime can you tell me what is pointing to this object?, but the situation is more complex because many objects in my system refer to the VM object, there is a complicated graph of references. If anything points into that graph, the whole thing stays live. Help or advice on solving this problem is welcome.
Re: Debugging a Memory Leak
GC problems are *nasty*. My advice is to run the simplest program you can think of that still exhibits the problem, and then put in printf debugging everywhere to see where it breaks down. Not sure if this is useful. Unfortunately, the program doesn't break or crash. It just keeps allocating memory that doesn't get freed. There must be some false reference somewhere. I'm not sure how I can printf debug my way out of that.
Destructor/Finalizer Guarantees
I have a situation where I have a VM (virtual machine) object, and several GCRoot (garbage collector root objects). The GCRoots are structs and will register themselves into a linked list belonging to the VM. I've made it so they unregister themselves in their destructor. This works perfectly well for GC roots which are on the stack. However, recently, I ran into a case where I need GCRoots which are not on the stack. This is where things broke down. The VM object got destroyed before the GCRoots, and when these tried to unregister themselves, they accessed memory which had already been reclaimed (the dead VM). What I want to know is: what guarantees can I expect from destructor behavior? I was thinking that when the VM gets destroyed, it could unregister all of its GCRoots at once. Then, when these are destroyed, they wouldn't try to touch the VM object. However, this only works if I can assume that the GC will first call the destructor on an object, then free the object, that this is done in a predictable order. Am I on the right track, or do I need to rethink this?
Capture parameter identifier name in a template?
In my JavaScript VM, I have a function whose purpose is to expose D/host constants to the JavaScript runtime code running inside the VM. This makes for somewhat redundant code, as follows: vm.defRTConst(OBJ_MIN_CAPw, OBJ_MIN_CAP); vm.defRTConst(PROTO_SLOT_IDXw, PROTO_SLOT_IDX); vm.defRTConst(FPTR_SLOT_IDXw, FPTR_SLOT_IDX); vm.defRTConst(ATTR_CONFIGURABLEw , ATTR_CONFIGURABLE); vm.defRTConst(ATTR_WRITABLEw , ATTR_WRITABLE); vm.defRTConst(ATTR_ENUMERABLEw, ATTR_ENUMERABLE); vm.defRTConst(ATTR_DELETEDw , ATTR_DELETED); vm.defRTConst(ATTR_GETSETw, ATTR_GETSET); vm.defRTConst(ATTR_DEFAULTw , ATTR_DEFAULT); I'm just wondering if there's a way to template defRTConst so that the name of an identifier I'm passing (e.g.: ATTR_DEFAULT) can be captured by the template, making it so that I don't also need to pass the name as a string. I expect the answer to be no, but maybe someone with more knowledge of D template magic knows better.
Introspecting a Module with Traits, allMembers
Hello, I'm looking to introspect a module, list all the members, iterate over them and filter them by kind inside of a static constructor. This is in the hope of shortening some hand-written code that is quite repetitive (adding many struct instances to an associative array in a static constructor). The code I'm trying to improve upon can be seen here: https://github.com/maximecb/Higgs/blob/master/source/ir/iir.d#L56 I've done some googling, and it seems I should be able to use the allMembers trait (http://wiki.dlang.org/Finding_all_Functions_in_a_Module), but unfortunately, the module name seems to be unrecognized, no matter which way I spell it: auto members = [__traits(allMembers, ir.ir)]; pragma(msg, members); Produces: ir/iir.d(85): Error: argument has no members Other people seem to have run into this problem. Am I doing it wrong or is this a bug in DMD?
Re: Introspecting a Module with Traits, allMembers
On Wednesday, 9 July 2014 at 20:07:57 UTC, NCrashed wrote: On Wednesday, 9 July 2014 at 20:04:47 UTC, Maxime Chevalier-Boisvert wrote: auto members = [__traits(allMembers, ir.ir)]; pragma(msg, members); Have you tried without quotes? pragma(msg, __traits(allMembers, ir.ir)); Did need to write it without the quotes, and to add enum to force compile-time evaluation. It's actually ir.ops that I wanted to list the members of. Got the following snippet to work: static this() { enum members = [__traits(allMembers, ir.ops)]; pragma(msg, members); } Prints: [object, ir, jit, OpArg, OpInfo, Opcode, GET_ARG, SET_STR, MAKE_VALUE, GET_WORD, GET_TYPE, IS_I32, ...]
Re: Introspecting a Module with Traits, allMembers
I got the following code to do what I want: static this() { void addOp(ref Opcode op) { assert ( op.mnem !in iir, duplicate op name ~ op.mnem ); iir[op.mnem] = op; } foreach (memberName; __traits(allMembers, ir.ops)) { static if (__traits(compiles, addOp(__traits(getMember, ir.ops, memberName { writeln(memberName); addOp(__traits(getMember, ir.ops, memberName)); } } } It's a bit of a hack, but it works. Is there any way to create some sort of alias for __traits(getMember, ir.ops, memberName) so that I don't have to write it out in full twice? Made some attempts but only got the compiler to complain.
Templating with Function Pointers?
I'd like to know if it's possible to create a templated function that takes a function pointer as an argument, and uses static ifs to generate different behaviors based on the type signature of the function pointer. More specifically, I want to create code generation functions for my JIT compiler, and template them based on a pointer to a function that implements the semantics of the operation being jitted. Not all of these operations take the same arguments, so I'd like to be able to template and generate different machine code. Is it possible to template based on a function pointer argument? If so, is it possible to use static ifs to know the return type or the argument types of the function pointer's signature, as well as how many arguments it takes?
Re: Templating with Function Pointers?
Thanks, I think the std.concurrency code demonstrates exactly what I need :) On Thursday, 12 December 2013 at 05:07:11 UTC, Ali Çehreli wrote: On 12/11/2013 06:43 PM, Maxime Chevalier-Boisvert wrote: Is it possible to template based on a function pointer argument? If so, is it possible to use static ifs to know the return type or the argument types of the function pointer's signature, as well as how many arguments it takes? That one is pretty straightforward: import std.string; void temp(Func)(Func func) { static if (is (Func == int function(double))) { pragma(msg, Found an int(double)); } else static if (is (Func == ReturnT function(string, size_t), ReturnT)) { pragma(msg, format(This one returns %s, ReturnT.stringof)); } } int foo(double d) { return 42; } struct S {} S bar(string s, size_t st) { return S.init; } void main() { temp(foo); temp(bar); } It is also possible to use alias template parameters or variadic template parameters. You may want to look at std.concurrency's receive() function implementation for more ideas. Ali
Friend class and methods
Does D have something like the concept of friend classes and functions in C++? I'd like to have a function that can access private members of two classes at the same time.
Re: Friend class and methods
The closest is to put both classes and the function in the same module. Things all in the same module can see the private members of each other. Good enough for my needs. Thanks.
CTFE Memory Hogging Workaround?
One of the reasons I chose to use D for my project is that I was very excited about the prospect of using CTFE in mixin code. Unfortunately, there seems to be one (or several?) bugs causing CTFE to be very slow and to hog a huge amount of memory (multiple gigs of RAM and swap). I use the latest dmd and my mixin code uses an Appender!string object to produce a long string of D code containing multiple functions (~1500 lines). Is there some known fix for this? I need this to work now. Otherwise, I'm going to have to rewrite my CTFE/mixin code in Python or something, which would be a shame, and a waste of time on my part. This is not the first compiler bug I run into, and I'm starting to regret not implementing my project in C++ instead.
Array literal template parameter?
I need to pass an array literal as a template parameter. The reference on this website seems to imply this is possible, but doesn't illustrate it. The obvious way doesn't seem to work: mixin template MyTemplate(int[] arr) {} Error: arithmetic/string type expected for value-parameter, not int[] Is there no way to do this without using a mixin and CTFE? I don't think I can use tuple types because I need to initialize a struct member using the array literal parameter.
Incrementing a variable in CTFE?
I'm using the mixin statement to generate function declarations based on data structure descriptions (my attempt at declarative programming). I'd like to be able to increment a variable each time I evaluate my code generation function in the mixin statement. This is so I can generate a unique id number for each data structure. Is there any way to do this? Could I increment a CTFE global somehow, or use something like C's static local variables?
unittest vs exceptions?
I'd like to write some unit tests to check my software. Writing unittest blocks and putting assert statements in there seems easy enough, but I noticed that if the code I run in there throws an exception, I don't get the unit test failed message. How does unittest check for success/failure? Does assert throw a special kind of error class when it fails? How would you check that some code didn't throw an exception, or did throw an exception?
Re: unittest vs exceptions?
These improvements would be very nice. The unit test framework, as it is, is rather underpowered. Exceptions could also use more documentation on the D website. I heard there was some exception chaining mechanism, but I can't even seem to find any info on the Error class. You shouldn't have to use a library to get named unit tests, since unit testing is built into D, and the lack of name unit tests is a real problem for stack traces and the like. It was suggested at one point that at minimum, unittest block functions have their line number in their name so that you can figure out which unittest block you're dealing with even if you can't name it yourself ( http://d.puremagic.com/issues/show_bug.cgi?id=5587 ), and not even that's been done yet. Though now that I read through that enhancement request again, it looks like theres an old pull request with the necessary changes in it ( https://github.com/D-Programming-Language/dmd/pull/264 ), but it's one of those that's been sitting around for a long time, probably because it's an enhancement rather than a bug fix, and Walter just hasn't gotten around to it. - Jonathan M Davis
instanceof?
Getting started with D. I've been doing alot of googling trying to find out what's the best way to check if an object is an instance of some class. So far, I found you could do: if (typeid(obj) == typeid(Class)) doSomething(); or: if (cast(Class)obj) doSomething(); These both seem a little clumsy, however. Is there a better way to do this?