Selectively instantiate covergroup

I am trying to selectivly instantiate certain covergroups based on a switch stored in the config_db database in UVM. Since config_db is only available in the build phase, and all embedded covergroups have to be new’ed in the class constructor as per the LRM, I was wondering if there is a way to selectively instantiate covergroups in the build phase using a config_db switch. Any suggestions are welcome.

You can call config_db#(my_object)::get(…) in the constructor. This means you will have to apply the settings before it gets constructed. You can also instantiate all your covergroups and selectively set the weights of particular instances to 0.

Another approach is to wrap one or more covergroups inside another class which can then be created during the build_phase (or any other phase) according to the config_db switch.

In reply to dave_59:

Hi Dave
I tried getting the config_db in new constructor . But it failed with UVM_FATAL error .
Is there anything else require while setting the config_db and getting it inside new constructor?

In reply to prat:

Don’t know how you could get UVM_FATAL from calling umm_config_db::get(). It would help to ask as a new question showing some code and the actual fatal message.

In reply to dave_59:

Hi Dave
I tried below

function new(string name = "<class_name>", uvm_component parent);
      super.new(name,parent);

     if (!uvm_config_db#(<class_name>_cfg)::get(this, "", "cfg", cfg))
        `uvm_fatal("no_cfg", "You must pass a cfg thru config_db!");

     vif=cfg.vif

     cover_group=new(vif);  //I want to pass the vif get using config_db to covergroup new'ed
endfunction:new

In reply to prat:

That’s your fatal error, not directly coming from calling umm_config_db::get.

How and when did you set it?

In reply to dave_59:
Below is the set for above config_db get

if (cfg.has_functional_coverage) begin
         cov = <class_name>::type_id::create("cov", this);
        uvm_config_db#(<class_name>_cfg)::set(this, "cov", "cfg", cfg);
  end

Actually
i have virtual interface which i am getting in uvm_subscriber build_phase() using config_db
I want to use this virtual interface in covergroups in uvm_subscriber class

 covergroup trans_cov_ @(posedge vif.clk_core iff vif.valid);
       coverbin __valid : coverpoint vif.valid {
      bins bin0 = {0,1};
    }
   endgroup // trans_cov

  function new(string name="<class_name>_coverage", uvm_component parent);
    super.new(name,parent);  
    trans_cov = new();   // this is what LRM recommand 
   endfunction : new

It not working when i access “vif.clk_core” in covergroup whose “vif” is visible (config_db get ) in build_phase

In reply to prat:

Hi Dave
What is the recommended way to pass the cofig_db get variables to covergroup which construct(new’ed) in new constructor of class before build phase?

covergroup cg  @(posedge  vif.clk)  // here this vif i got in build_phase.

new()
  cg = new();
end
build_phase
// confid_db get to get the virtual interface 
endphase