File: jthread.c

package info (click to toggle)
jmp 0.48-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,692 kB
  • ctags: 1,732
  • sloc: ansic: 13,985; sh: 8,611; makefile: 526; yacc: 318; java: 18
file content (260 lines) | stat: -rw-r--r-- 6,538 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <jvmpi.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <jthread.h>
#include <mvector.h>
#include <hash.h>
#include <obj.h>

static char* empty = "<unknown>";

jthread* jthread_new (const char* thread_name, const char* group_name, 
		      const char* parent_name, jobjectID thread_id,
		      JNIEnv* env_id, timerstack *s) {
    jthread* c;
    
    c = malloc (sizeof (*c));
    if (c == NULL)
	return NULL;
    
    if (thread_name)
	c->thread_name = strdup (thread_name);
    else 
	c->thread_name = empty;
    if (group_name)
	c->group_name = strdup (group_name);
    else 
	c->group_name = empty;
    if (parent_name)
	c->parent_name = strdup (parent_name);
    else 
	c->parent_name = empty;

    if (c->thread_name == NULL ||
	c->group_name == NULL ||
	c->parent_name == NULL) {
	jthread_free (c);
	return NULL;
    }
    
    c->thread_id = thread_id;
    c->env_id = env_id;
    c->timerstack = s;
    return c;
}

void jthread_free (jthread* t) {
    if (t == NULL)
	return;
    if (t->parent_name != empty)
	free (t->parent_name);
    if (t->group_name != empty)
	free (t->group_name);
    if (t->thread_name != empty)
	free (t->thread_name);
    free (t);
}

size_t jthread_jmphash_func (void* c, size_t len) {
    jthread* tp = (jthread*)c;
    return ((long)tp->env_id) % len;
}

int jthread_cmp_func (void* c1, void* c2) {
    jthread* tp1 = (jthread*)c1;
    jthread* tp2 = (jthread*)c2;
    return tp1->env_id != tp2->env_id;
}

JNIEnv* jthread_get_env_id (jthread* t) {
    return t->env_id;
}

void jthread_set_env_id (jthread* t, JNIEnv* env_id) {
    t->env_id = env_id;
}

void jthread_method_entry (timerstack* s, method *m, jlong tval) {
    methodtime* tvc;
    
    m->entered++;    

    timerstack_lock (s);
    s->cpu_time = tval;
    if (s->top == s->max) {
	fprintf (stderr, "time to expand timerstack: (%p, %d, %d)\n", s->times,
		 s->top, s->max);
	timerstack_expand (s);
	fprintf (stderr, "timerstack expanded: (%p, %d, %d)\n", s->times,
		 s->top, s->max);
    }
    
    tvc = s->times + s->top;
    s->top++;
    tvc->tv = tval;
    tvc->tv_hold = 0;
    tvc->method = m;
    if (allocs_follow_filter ()) {
	if (cls_get_filtered (method_get_owner (m))) {
	    tvc->filtered_method = m;
	} else {
	    if (s->top > 1) { /* we added one a few lines up... */
		methodtime* tvc_prev = s->times + s->top - 2;
		tvc->filtered_method = tvc_prev->filtered_method;
	    } else {
		tvc->filtered_method = m;
	    }
	}
    }
    timerstack_unlock (s);
}

void jthread_method_exit (timerstack* s, jmethodID method_id, jlong tval, JNIEnv* env) {
    jlong tdiff;
    methodtime* tvc = NULL;

    timerstack_lock (s);
    s->cpu_time = tval;
    if (s->top <= 0) {
	/** this should be rare, locking here should be ok.. /robo */
	fprintf (stderr, "jthread_method_exit: stack underflow, trying to get stack:\n");
	get_call_trace_env (env);
    }

    if (s->top > 0) {
	s->top--;
	tvc = s->times + s->top;
    }
    
    if (tvc) {
        method* m = tvc->method;
	if (m != NULL) {
	    if (m->method_id != method_id) {
		fprintf (stderr, "jthread_method_exit stack mismatch for %p "
			 "got id = %p  have id = %p, requesting stack...\n",
			 env, method_id, m->method_id);
		timerstack_unlock (s);
		get_call_trace_env (env);
		timerstack_lock (s);
		if (s->top > 0) {
		    s->top--;
		    tvc = s->times + s->top;
		}
		if (tvc) {
		    method* m = tvc->method; 
		    if (m == NULL)
			return;	
		    if (m->method_id != method_id) {
			fprintf (stderr, 
				 "jthread_method_exit stack stil in mismatch "
				 "for %p got id = %p  have id = %p\n",
				 env, method_id, m->method_id);
			return;
		    }
		}
	    }

	    /* update this methods total time. */
	    tdiff = tval - tvc->tv - tvc->tv_hold;
	    if (tdiff < 0) {
		/* Ok, this should normally not happen, but it can happen 
		 * when a thread has had its stack messed up / missing.
		 * negative values => incorrectly huge values for some 
		 * methods so handle it nicely...
		 */
		tdiff = 0;
	    }
	    m->calls++;
	    m->entered--;    
            m->time_used.tv += tdiff;
	    m->time_used.tv_hold += tvc->tv_hold;
	    method_set_modified (m, 1);
	
	    /* notify caller how much time this method took.. */
	    tdiff = tval - tvc->tv;
	
	    /* get caller and fill in values. */
	    if (s->top > 0) {
		method* mc; 
		jlong hold = tdiff;
		tvc = s->times + s->top - 1;
		mc = tvc->method;	    
		tvc->tv_hold += hold;
		/* not sure if this should be here? /robo
		   method_set_modified (mc, 1);
		*/
		if (mc && mc->called_methods) {
		    long i = mvector_search (mc->called_methods, m);
		    if (i == -1)
			i = mvector_add_method (mc->called_methods, m);
		    if (m->callee_methods) {
			i = mvector_search (m->callee_methods, mc);
			if (i == -1)
			    i = mvector_add_method (m->callee_methods, mc);
		    }
		}
	    } else {
		tvc = NULL;
	    }
	} else {
	    fprintf (stderr,
		     "jthread_method_exit: exited method (%p) is null, env = %p\n", 
		     method_id, env);
	}
	/* if we have no parent method we just ignore for now. 
	   Im not sure if we should take actions */
    }

    timerstack_unlock (s);
}


/** The given thread has is waiting for a monitor. */
void jthread_contenation_enter (timerstack* s,
				obj* monitor,
				jlong tval) {
    timerstack_lock (s);
    s->last_contentation = tval;
    timerstack_unlock (s);
}

/** The given thread got the monitor it was waiting for. */
void jthread_contenation_entered (timerstack* s,
				  obj* monitor,
				  jlong tval) {
    timerstack_lock (s);
    if (s->last_contentation != -1)
	s->contendtime += tval - s->last_contentation;
    else 
	fprintf (stderr, "jthread_contenation_entered: stack underflow\n");
    /* I dont think we need it, but maybe we should do this: 
    s->last_contentation = -1;
    */
    timerstack_unlock (s);
}

char* jthread_get_thread_name (jthread* t) {
    return (t->thread_name);
}
char* jthread_get_group_name (jthread* t) {
    return (t->group_name);
}
char* jthread_get_parent_name (jthread* t) {
    return (t->parent_name);
}


/* Emacs Local Variables: */
/* Emacs mode:C */
/* Emacs c-indentation-style:"gnu" */
/* Emacs c-hanging-braces-alist:((brace-list-open)(brace-entry-open)(defun-open after)(substatement-open after)(block-close . c-snug-do-while)(extern-lang-open after)) */
/* Emacs c-cleanup-list:(brace-else-brace brace-elseif-brace space-before-funcall) */
/* Emacs c-basic-offset:4 */
/* Emacs End: */