Skip to content

Commit

Permalink
Merge branch 'master' of github.com:JuliaLang/julia
Browse files Browse the repository at this point in the history
* 'master' of github.com:JuliaLang/julia:
  adding exports for gc control functions
  allow arithmetic on Bool, promoting to Int useful for masking and counting re: issue #115
  adding some heap profiling code that counts objects by type
  • Loading branch information
StefanKarpinski committed Jan 3, 2012
2 parents a7806d0 + 0afa756 commit e8b547a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
21 changes: 21 additions & 0 deletions j/bool.j
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

convert(::Type{Bool}, x::Number) = (x!=0)

# promote Bool to any other numeric type
promote_rule{T<:Number}(::Type{Bool}, ::Type{T}) = T

bool(x) = true
bool(x::Bool) = x
bool(x::Number) = convert(Bool, x)
Expand Down Expand Up @@ -32,3 +35,21 @@ count(x::Bool, y::Bool) = count(x) + count(y)

count(x::Integer, y::Bool) = x + count(y)
count(x::Bool, y::Integer) = count(x) + y

## do arithmetic as Int ##

<(x::Bool, y::Bool) = y&!x
==(x::Bool, y::Bool) = eq_int(unbox8(x),unbox8(y))

-(x::Bool) = -int(x)

+(x::Bool, y::Bool) = int(x)+int(y)
-(x::Bool, y::Bool) = int(x)-int(y)
*(x::Bool, y::Bool) = int(x)*int(y)
/(x::Bool, y::Bool) = int(x)/int(y)
^(x::Bool, y::Bool) = int(x)^int(y)

div(x::Bool, y::Bool) = div(int(x),int(y))
fld(x::Bool, y::Bool) = fld(int(x),int(y))
rem(x::Bool, y::Bool) = rem(int(x),int(y))
mod(x::Bool, y::Bool) = mod(int(x),int(y))
1 change: 1 addition & 0 deletions j/rational.j
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function convert{T<:Integer}(::Type{Rational{T}}, x::Float, tol::Real)
end
end
convert{T<:Integer}(rt::Type{Rational{T}}, x::Float) = convert(rt,x,0)
convert(::Type{Bool}, x::Rational) = (x!=0) # to resolve ambiguity
convert{T<:Real}(::Type{T}, x::Rational) = convert(T, x.num/x.den)

promote_rule{T<:Integer}(::Type{Rational{T}}, ::Type{T}) = Rational{T}
Expand Down
4 changes: 2 additions & 2 deletions src/boot.j
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@
type Nothing; end
const nothing = Nothing()

bitstype 8 Bool

abstract Number
abstract Real <: Number
abstract Float <: Real
abstract Integer <: Real
abstract Signed <: Integer
abstract Unsigned <: Integer

bitstype 8 Bool <: Integer

bitstype 32 Float32 <: Float
bitstype 64 Float64 <: Float

Expand Down
50 changes: 50 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@

// with MEMDEBUG, every object is allocated explicitly with malloc, and
// filled with 0xbb before being freed.
// NOTE: needs to be defined in ios.c too, due to GC/IObuffer interaction
//#define MEMDEBUG

// MEMPROFILE prints pool summary statistics after every GC
// NOTE: define in ios.c too
//#define MEMPROFILE

// GCTIME prints time taken by each phase of GC
//#define GCTIME

// OBJPROFILE counts objects by type
//#define OBJPROFILE

#define GC_PAGE_SZ (1536*sizeof(void*))//bytes

typedef struct _gcpage_t {
Expand Down Expand Up @@ -102,6 +111,10 @@ static arraylist_t preserved_values;

static arraylist_t weak_refs;

#ifdef OBJPROFILE
static htable_t obj_counts;
#endif

int jl_gc_n_preserved_values(void)
{
return preserved_values.len;
Expand Down Expand Up @@ -456,6 +469,13 @@ static void gc_markval_(jl_value_t *v)
//assert(v != lookforme);
if (gc_marked_obj(v)) return;
jl_value_t *vt = (jl_value_t*)jl_typeof(v);
#ifdef OBJPROFILE
void **bp = ptrhash_bp(&obj_counts, vt);
if (*bp == HT_NOTFOUND)
*bp = (void*)2;
else
(*((ptrint_t*)bp))++;
#endif
jl_value_t *vtt = gc_typeof(vt);
gc_setmark_obj(v);

Expand Down Expand Up @@ -677,6 +697,28 @@ static void all_pool_stats(void);
static void big_obj_stats(void);
#endif

#ifdef OBJPROFILE
static void print_obj_profile(void)
{
jl_value_t *errstream = jl_get_global(jl_system_module,
jl_symbol("stderr_stream"));
JL_TRY {
if (errstream)
jl_set_current_output_stream_obj(errstream);
ios_t *s = jl_current_output_stream();
for(int i=0; i < obj_counts.size; i+=2) {
if (obj_counts.table[i+1] != HT_NOTFOUND) {
ios_printf(s, "%d ", obj_counts.table[i+1]-1);
jl_show(obj_counts.table[i]);
ios_printf(s, "\n");
}
}
}
JL_CATCH {
}
}
#endif

void jl_gc_collect(void)
{
allocd_bytes = 0;
Expand All @@ -703,6 +745,10 @@ void jl_gc_collect(void)
#endif
run_finalizers();
JL_SIGATOMIC_END();
#ifdef OBJPROFILE
print_obj_profile();
htable_reset(&obj_counts, 0);
#endif
}
}

Expand Down Expand Up @@ -804,6 +850,10 @@ void jl_gc_init(void)
arraylist_new(&to_finalize, 0);
arraylist_new(&preserved_values, 0);
arraylist_new(&weak_refs, 0);

#ifdef OBJPROFILE
htable_new(&obj_counts, 0);
#endif
}

#if defined(MEMPROFILE)
Expand Down
3 changes: 3 additions & 0 deletions src/julia.expmap
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
jl_gc_add_finalizer;
jl_gc_collect;
jl_gc_lookfor;
jl_gc_enable;
jl_gc_disable;
jl_gc_is_enabled;
jl_errno;
jl_strerror;
jl_environ;
Expand Down

0 comments on commit e8b547a

Please sign in to comment.