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

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

Overview

Placeholder for code coverage collection Implementation in ext/datadog_cov

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

DDCov instance methods available in Ruby



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
342
343
344
345
346
347
348
349
350
351
# File 'ext/datadog_cov/datadog_cov.c', line 299

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 = 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 = 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



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
380
381
382
383
# File 'ext/datadog_cov/datadog_cov.c', line 354

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



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'ext/datadog_cov/datadog_cov.c', line 387

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, process_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;
}