Class: Datadog::CI::TestOptimisation::Coverage::DDCov

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/test_optimisation/coverage/ddcov.rb,
ext/datadog_ci_native/datadog_cov.c

Overview

Placeholder for code coverage collection Implementation in ext/datadog_ci_native/datadog_cov.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

DDCov instance methods available in Ruby



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'ext/datadog_ci_native/datadog_cov.c', line 264

static VALUE dd_cov_initialize(int argc, VALUE *argv, VALUE self) {
  VALUE opt;

  rb_scan_args(argc, argv, "10", &opt);
  VALUE rb_root = rb_hash_lookup(opt, ID2SYM(rb_intern("root")));
  if (!RTEST(rb_root)) {
    rb_raise(rb_eArgError, "root is required");
  }
  VALUE rb_ignored_path =
      rb_hash_lookup(opt, ID2SYM(rb_intern("ignored_path")));

  VALUE rb_threading_mode =
      rb_hash_lookup(opt, ID2SYM(rb_intern("threading_mode")));
  enum threading_mode threading_mode;
  if (rb_threading_mode == ID2SYM(rb_intern("multi"))) {
    threading_mode = multi;
  } else if (rb_threading_mode == ID2SYM(rb_intern("single"))) {
    threading_mode = single;
  } else {
    rb_raise(rb_eArgError, "threading mode is invalid");
  }

  VALUE rb_allocation_tracing_enabled =
      rb_hash_lookup(opt, ID2SYM(rb_intern("use_allocation_tracing")));
  if (rb_allocation_tracing_enabled == Qtrue && threading_mode == single) {
    rb_raise(rb_eArgError,
             "allocation tracing is not supported in single threaded mode");
  }

  struct dd_cov_data *dd_cov_data;
  TypedData_Get_Struct(self, struct dd_cov_data, &dd_cov_data_type,
                       dd_cov_data);

  dd_cov_data->threading_mode = threading_mode;
  dd_cov_data->root_len = RSTRING_LEN(rb_root);
  dd_cov_data->root =
      dd_ci_ruby_strndup(RSTRING_PTR(rb_root), dd_cov_data->root_len);

  if (RTEST(rb_ignored_path)) {
    dd_cov_data->ignored_path_len = RSTRING_LEN(rb_ignored_path);
    dd_cov_data->ignored_path = dd_ci_ruby_strndup(
        RSTRING_PTR(rb_ignored_path), dd_cov_data->ignored_path_len);
  }

  if (rb_allocation_tracing_enabled == Qtrue) {
    dd_cov_data->object_allocation_tracepoint = rb_tracepoint_new(
        Qnil, RUBY_INTERNAL_EVENT_NEWOBJ, on_newobj_event, (void *)dd_cov_data);
  }

  return Qnil;
}

Instance Method Details

#startObject

starts test impact collection, executed before the start of each test



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'ext/datadog_ci_native/datadog_cov.c', line 317

static VALUE dd_cov_start(VALUE self) {
  struct dd_cov_data *dd_cov_data;
  TypedData_Get_Struct(self, struct dd_cov_data, &dd_cov_data_type,
                       dd_cov_data);

  if (dd_cov_data->root_len == 0) {
    rb_raise(rb_eRuntimeError, "root is required");
  }

  // add line tracepoint
  if (dd_cov_data->threading_mode == single) {
    VALUE thval = rb_thread_current();
    rb_thread_add_event_hook(thval, on_line_event, RUBY_EVENT_LINE, self);
    dd_cov_data->th_covered = thval;
  } else {
    rb_add_event_hook(on_line_event, RUBY_EVENT_LINE, self);
  }

  // add object allocation tracepoint
  if (dd_cov_data->object_allocation_tracepoint != Qnil) {
    rb_tracepoint_enable(dd_cov_data->object_allocation_tracepoint);
  }

  return self;
}

#stopObject

returns the hash with impacted files and resets the internal state



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'ext/datadog_ci_native/datadog_cov.c', line 345

static VALUE dd_cov_stop(VALUE self) {
  struct dd_cov_data *dd_cov_data;
  TypedData_Get_Struct(self, struct dd_cov_data, &dd_cov_data_type,
                       dd_cov_data);

  // stop line tracepoint
  if (dd_cov_data->threading_mode == single) {
    VALUE thval = rb_thread_current();
    if (!rb_equal(thval, dd_cov_data->th_covered)) {
      rb_raise(rb_eRuntimeError, "Coverage was not started by this thread");
    }

    rb_thread_remove_event_hook(dd_cov_data->th_covered, on_line_event);
    dd_cov_data->th_covered = Qnil;
  } else {
    rb_remove_event_hook(on_line_event);
  }

  // stop object allocation tracepoint
  if (dd_cov_data->object_allocation_tracepoint != Qnil) {
    rb_tracepoint_disable(dd_cov_data->object_allocation_tracepoint);
  }

  // process classes covered by allocation tracing
  st_foreach(dd_cov_data->klasses_table, each_instantiated_klass,
             (st_data_t)dd_cov_data);
  st_clear(dd_cov_data->klasses_table);

  VALUE res = dd_cov_data->impacted_files;

  dd_cov_data->impacted_files = rb_hash_new();
  dd_cov_data->last_filename_ptr = 0;

  return res;
}