Skip to content

Commit 13e9761

Browse files
Merge pull request #899 from Unity-Technologies/unity-master-fileio-profiling
File IO profile support
2 parents 17a6516 + aafa4ac commit 13e9761

File tree

9 files changed

+104
-3
lines changed

9 files changed

+104
-3
lines changed

mono/metadata/profiler-events.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ MONO_PROFILER_EVENT_1(gc_finalized_object, GCFinalizedObject, MonoObject *, obje
9595
MONO_PROFILER_EVENT_5(gc_root_register, RootRegister, const mono_byte *, start, uintptr_t, size, MonoGCRootSource, source, const void *, key, const char *, name)
9696
MONO_PROFILER_EVENT_1(gc_root_unregister, RootUnregister, const mono_byte *, start)
9797
MONO_PROFILER_EVENT_3(gc_roots, GCRoots, uint64_t, count, const mono_byte *const *, addresses, MonoObject *const *, objects)
98+
MONO_PROFILER_EVENT_2(fileio, FileIO, uint64_t, kind, uint64_t, size)
9899

99100
MONO_PROFILER_EVENT_1(monitor_contention, MonitorContention, MonoObject *, object)
100101
MONO_PROFILER_EVENT_1(monitor_failed, MonitorFailed, MonoObject *, object)

mono/metadata/profiler-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef struct {
5959
guint32 sample_freq;
6060

6161
gboolean allocations;
62+
gboolean fileio;
6263

6364
gboolean call_contexts;
6465
void (*context_enable) (void);

mono/metadata/profiler.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,12 @@ mono_profiler_enable_allocations (void)
567567
return mono_profiler_state.allocations = TRUE;
568568
}
569569

570+
mono_bool
571+
mono_profiler_enable_fileio (void)
572+
{
573+
return mono_profiler_state.fileio = TRUE;
574+
}
575+
570576
/**
571577
* mono_profiler_set_call_instrumentation_filter_callback:
572578
*
@@ -952,6 +958,7 @@ typedef void (*MonoLegacyProfileAllocFunc) (MonoLegacyProfiler *prof, MonoObject
952958
typedef void (*MonoLegacyProfileMethodFunc) (MonoLegacyProfiler *prof, MonoMethod *method);
953959
typedef void (*MonoLegacyProfileExceptionFunc) (MonoLegacyProfiler *prof, MonoObject *object);
954960
typedef void (*MonoLegacyProfileExceptionClauseFunc) (MonoLegacyProfiler *prof, MonoMethod *method, int clause_type, int clause_num);
961+
typedef void (*MonoLegacyProfileFileIOFunc) (MonoLegacyProfiler *prof, int kind, int size);
955962

956963
struct _MonoProfiler {
957964
MonoProfilerHandle handle;
@@ -962,6 +969,7 @@ struct _MonoProfiler {
962969
MonoLegacyProfileGCResizeFunc gc_heap_resize;
963970
MonoLegacyProfileJitResult jit_end2;
964971
MonoLegacyProfileAllocFunc allocation;
972+
MonoLegacyProfileFileIOFunc fileio;
965973
MonoLegacyProfileMethodFunc enter;
966974
MonoLegacyProfileMethodFunc leave;
967975
MonoLegacyProfileExceptionFunc throw_callback;
@@ -978,6 +986,7 @@ MONO_API void mono_profiler_install_jit_end (MonoLegacyProfileJitResult end);
978986
MONO_API void mono_profiler_set_events (int flags);
979987
MONO_API void mono_profiler_install_allocation (MonoLegacyProfileAllocFunc callback);
980988
MONO_API void mono_profiler_install_enter_leave (MonoLegacyProfileMethodFunc enter, MonoLegacyProfileMethodFunc fleave);
989+
MONO_API void mono_profiler_install_fileio (MonoLegacyProfileFileIOFunc callback);
981990
MONO_API void mono_profiler_install_exception (MonoLegacyProfileExceptionFunc throw_callback, MonoLegacyProfileMethodFunc exc_method_leave, MonoLegacyProfileExceptionClauseFunc clause_callback);
982991

983992
static void
@@ -1096,7 +1105,8 @@ typedef enum
10961105
MONO_PROFILE_ENTER_LEAVE = 1 << 12,
10971106
MONO_PROFILE_COVERAGE = 1 << 13,
10981107
MONO_PROFILE_INS_COVERAGE = 1 << 14,
1099-
MONO_PROFILE_STATISTICAL = 1 << 15
1108+
MONO_PROFILE_STATISTICAL = 1 << 15,
1109+
MONO_PROFILE_FILEIO = 1 << 16
11001110
} LegacyMonoProfileFlags;
11011111

11021112
void
@@ -1110,6 +1120,9 @@ mono_profiler_set_events (int flags)
11101120

11111121
if (flags & MONO_PROFILE_ALLOCATIONS)
11121122
mono_profiler_enable_allocations ();
1123+
1124+
if (flags & MONO_PROFILE_FILEIO)
1125+
mono_profiler_enable_fileio ();
11131126
}
11141127

11151128
static void
@@ -1127,6 +1140,21 @@ mono_profiler_install_allocation (MonoLegacyProfileAllocFunc callback)
11271140
mono_profiler_set_gc_allocation_callback (current->handle, allocation_cb);
11281141
}
11291142

1143+
static void
1144+
fileio_cb (MonoProfiler *prof, uint64_t kind, uint64_t size)
1145+
{
1146+
prof->fileio (prof->profiler, kind, size);
1147+
}
1148+
1149+
void
1150+
mono_profiler_install_fileio (MonoLegacyProfileFileIOFunc callback)
1151+
{
1152+
current->fileio = callback;
1153+
1154+
if (callback)
1155+
mono_profiler_set_fileio_callback (current->handle, fileio_cb);
1156+
}
1157+
11301158
static void
11311159
enter_cb (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *context)
11321160
{

mono/metadata/profiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ MONO_API mono_bool mono_profiler_set_sample_mode (MonoProfilerHandle handle, Mon
7070
MONO_API mono_bool mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint32_t *freq);
7171

7272
MONO_API mono_bool mono_profiler_enable_allocations (void);
73+
MONO_API mono_bool mono_profiler_enable_fileio (void);
7374

7475
typedef struct _MonoProfilerCallContext MonoProfilerCallContext;
7576

mono/metadata/w32file-unix.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "w32file-unix-glob.h"
4141
#include "w32error.h"
4242
#include "fdhandle.h"
43+
#include "mono/metadata/profiler-private.h"
4344
#include "utils/mono-io-portability.h"
4445
#include "utils/mono-logger-internals.h"
4546
#include "utils/mono-os-mutex.h"
@@ -1037,6 +1038,7 @@ file_read(FileHandle *filehandle, gpointer buffer, guint32 numbytes, guint32 *by
10371038

10381039
if (bytesread != NULL) {
10391040
*bytesread = ret;
1041+
MONO_PROFILER_RAISE (fileio, (1, *bytesread));
10401042
}
10411043

10421044
return(TRUE);
@@ -1104,6 +1106,7 @@ file_write(FileHandle *filehandle, gconstpointer buffer, guint32 numbytes, guint
11041106
}
11051107
if (byteswritten != NULL) {
11061108
*byteswritten = ret;
1109+
MONO_PROFILER_RAISE (fileio, (0, *byteswritten));
11071110
}
11081111
return(TRUE);
11091112
}

mono/metadata/w32file-win32.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <winsock2.h>
1212
#include <windows.h>
1313
#include "mono/metadata/w32file-win32-internals.h"
14+
#include "mono/metadata/profiler-private.h"
1415

1516
void
1617
mono_w32file_init (void)
@@ -87,6 +88,7 @@ mono_w32file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *b
8788
gboolean res;
8889
MONO_ENTER_GC_SAFE;
8990
res = ReadFile (handle, buffer, numbytes, bytesread, NULL);
91+
MONO_PROFILER_RAISE (fileio, (1, *bytesread));
9092
MONO_EXIT_GC_SAFE;
9193
return res;
9294
}
@@ -97,6 +99,7 @@ mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, gui
9799
gboolean res;
98100
MONO_ENTER_GC_SAFE;
99101
res = WriteFile (handle, buffer, numbytes, byteswritten, NULL);
102+
MONO_PROFILER_RAISE (fileio, (0, *byteswritten));
100103
MONO_EXIT_GC_SAFE;
101104
return res;
102105
}

mono/profiler/log-args.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static NameAndMask event_list[] = {
2727

2828
{ "counters", PROFLOG_COUNTER_EVENTS },
2929
{ "alloc", PROFLOG_ALLOC_ALIAS, PROFLOG_ALLOC_ALIAS | PROFLOG_GC_ROOT_EVENTS },
30+
{ "fileio", PROFLOG_FILEIO_EVENTS },
3031
{ "legacy", PROFLOG_LEGACY_ALIAS },
3132
};
3233

mono/profiler/log.c

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ static gint32 sync_points_ctr,
112112
coverage_methods_ctr,
113113
coverage_statements_ctr,
114114
coverage_classes_ctr,
115-
coverage_assemblies_ctr;
115+
coverage_assemblies_ctr,
116+
fileio_reads_ctr,
117+
fileio_writes_ctr;
118+
116119

117120
// Pending data to be written to the log, for a single thread.
118121
// Threads periodically flush their own LogBuffers by calling safe_send
@@ -1656,6 +1659,37 @@ finalize_object_end (MonoProfiler *prof, MonoObject *obj)
16561659
EXIT_LOG;
16571660
}
16581661

1662+
static void
1663+
fileio (MonoProfiler *prof, uint64_t kind, uint64_t size)
1664+
{
1665+
if (kind == 0)
1666+
{
1667+
ENTER_LOG (&fileio_writes_ctr, buf,
1668+
EVENT_SIZE /* event */ +
1669+
LEB128_SIZE /* kind */ +
1670+
LEB128_SIZE /* size */
1671+
);
1672+
emit_event (buf, TYPE_FILEIO);
1673+
emit_value (buf, kind);
1674+
emit_value (buf, size);
1675+
1676+
EXIT_LOG;
1677+
}
1678+
else
1679+
{
1680+
ENTER_LOG (&fileio_reads_ctr, buf,
1681+
EVENT_SIZE /* event */ +
1682+
LEB128_SIZE /* kind */ +
1683+
LEB128_SIZE /* size */
1684+
);
1685+
emit_event (buf, TYPE_FILEIO);
1686+
emit_value (buf, kind);
1687+
emit_value (buf, size);
1688+
1689+
EXIT_LOG;
1690+
}
1691+
}
1692+
16591693
static char*
16601694
push_nesting (char *p, MonoClass *klass)
16611695
{
@@ -4416,6 +4450,28 @@ proflog_icall_SetGCAllocationEvents (MonoBoolean value)
44164450
mono_coop_mutex_unlock (&log_profiler.api_mutex);
44174451
}
44184452

4453+
ICALL_EXPORT MonoBoolean
4454+
proflog_icall_GetFileIOEvents (void)
4455+
{
4456+
return ENABLED (PROFLOG_FILEIO_EVENTS);
4457+
}
4458+
4459+
ICALL_EXPORT void
4460+
proflog_icall_SetFileIOEvents (MonoBoolean value)
4461+
{
4462+
mono_coop_mutex_lock (&log_profiler.api_mutex);
4463+
4464+
if (value) {
4465+
ENABLE (PROFLOG_FILEIO_EVENTS);
4466+
mono_profiler_set_fileio_callback (log_profiler.handle, fileio);
4467+
} else {
4468+
DISABLE (PROFLOG_FILEIO_EVENTS);
4469+
mono_profiler_set_fileio_callback (log_profiler.handle, NULL);
4470+
}
4471+
4472+
mono_coop_mutex_unlock (&log_profiler.api_mutex);
4473+
}
4474+
44194475
ICALL_EXPORT MonoBoolean
44204476
proflog_icall_GetGCMoveEvents (void)
44214477
{
@@ -4604,6 +4660,8 @@ runtime_initialized (MonoProfiler *profiler)
46044660
register_counter ("Event: Coverage statements", &coverage_statements_ctr);
46054661
register_counter ("Event: Coverage classes", &coverage_classes_ctr);
46064662
register_counter ("Event: Coverage assemblies", &coverage_assemblies_ctr);
4663+
register_counter ("Event: File IO reads", &fileio_reads_ctr);
4664+
register_counter ("Event: File IO writes", &fileio_writes_ctr);
46074665

46084666
counters_init ();
46094667

@@ -4848,6 +4906,9 @@ mono_profiler_init_log (const char *desc)
48484906
mono_profiler_set_gc_finalizing_object_callback (handle, finalize_object_begin);
48494907
}
48504908

4909+
if (ENABLED (PROFLOG_FILEIO_EVENTS))
4910+
mono_profiler_set_fileio_callback (handle, fileio);
4911+
48514912
//On Demand heapshot uses the finalizer thread to force a collection and thus a heapshot
48524913
mono_profiler_set_gc_finalized_callback (handle, finalize_end);
48534914

mono/profiler/log.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ enum {
386386
TYPE_RUNTIME,
387387
TYPE_COVERAGE,
388388
TYPE_META,
389+
TYPE_FILEIO,
389390
/* extended type for TYPE_HEAP */
390391
TYPE_HEAP_START = 0 << 4,
391392
TYPE_HEAP_END = 1 << 4,
@@ -491,10 +492,11 @@ typedef enum {
491492
#define PROFLOG_COUNTER_EVENTS (1 << 8)
492493
#define PROFLOG_SAMPLE_EVENTS (1 << 9)
493494
#define PROFLOG_JIT_EVENTS (1 << 10)
495+
#define PROFLOG_FILEIO_EVENTS (1 << 11)
494496

495497
#define PROFLOG_ALLOC_ALIAS (PROFLOG_GC_EVENTS | PROFLOG_GC_ALLOCATION_EVENTS | PROFLOG_GC_MOVE_EVENTS)
496498
#define PROFLOG_HEAPSHOT_ALIAS (PROFLOG_GC_EVENTS | PROFLOG_GC_ROOT_EVENTS)
497-
#define PROFLOG_LEGACY_ALIAS (PROFLOG_EXCEPTION_EVENTS | PROFLOG_MONITOR_EVENTS | PROFLOG_GC_EVENTS | PROFLOG_GC_MOVE_EVENTS | PROFLOG_GC_ROOT_EVENTS | PROFLOG_GC_HANDLE_EVENTS | PROFLOG_GC_FINALIZATION_EVENTS | PROFLOG_COUNTER_EVENTS)
499+
#define PROFLOG_LEGACY_ALIAS (PROFLOG_EXCEPTION_EVENTS | PROFLOG_MONITOR_EVENTS | PROFLOG_GC_EVENTS | PROFLOG_GC_MOVE_EVENTS | PROFLOG_GC_ROOT_EVENTS | PROFLOG_GC_HANDLE_EVENTS | PROFLOG_GC_FINALIZATION_EVENTS | PROFLOG_COUNTER_EVENTS | PROFLOG_FILEIO_EVENTS)
498500

499501
typedef struct {
500502
//Events explicitly enabled

0 commit comments

Comments
 (0)