LCOV - code coverage report
Current view: top level - OdbDesignLib/ProductModel - Design.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 86.1 % 467 402
Test Date: 2026-11-19 04:56:22 Functions: 87.2 % 47 41
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "Design.h"
       2              : #include "Design.h"
       3              : #include "Package.h"
       4              : #include "Logger.h"
       5              : #include "../enums.h"
       6              : #include "Part.h"
       7              : #include "ProtobufMapHelpers.h"
       8              : #include <cstdint>
       9              : #include <memory>
      10              : #include "Net.h"
      11              : #include "../FileModel/Design/FileArchive.h"
      12              : #include "../FileModel/Design/EdaDataFile.h"
      13              : #include "../Text/Utf8Sanitizer.h"
      14              : 
      15              : 
      16              : namespace Odb::Lib::ProductModel
      17              : {       
      18           64 :         Design::Design()
      19              :         {
      20           64 :         }
      21              : 
      22           68 :         Design::~Design()
      23              :         {
      24           65 :                 m_components.clear();
      25           65 :                 m_componentsByName.clear();
      26           65 :                 m_nets.clear();
      27           65 :                 m_netsByName.clear();
      28           65 :                 m_packages.clear();
      29           65 :                 m_packagesByName.clear();
      30           65 :                 m_parts.clear();
      31           65 :                 m_partsByName.clear();
      32           68 :         }
      33              : 
      34           21 :         const std::string& Design::GetName() const
      35              :         {
      36           21 :                 return m_name;
      37              :         }
      38              : 
      39           21 :         const std::string& Design::GetProductModel() const
      40              :         {
      41           21 :                 return m_productModel;
      42              :         }
      43              : 
      44         8391 :         const Net::Vector& Design::GetNets() const
      45              :         {
      46         8391 :                 return m_nets;
      47              :         }
      48              : 
      49            2 :         const Net::StringMap Design::GetNetsByName() const
      50              :         {
      51            2 :                 return m_netsByName;
      52              :         }
      53              : 
      54          942 :         const Package::Vector& Design::GetPackages() const
      55              :         {
      56          942 :                 return m_packages;
      57              :         }
      58              : 
      59            2 :         const Package::StringMap& Design::GetPackagesByName() const
      60              :         {
      61            2 :                 return m_packagesByName;
      62              :         }
      63              : 
      64        10591 :         const Component::Vector& Design::GetComponents() const
      65              :         {
      66        10591 :                 return m_components;
      67              :         }
      68              : 
      69            2 :         const Component::StringMap& Design::GetComponentsByName() const
      70              :         {
      71            2 :                 return m_componentsByName;
      72              :         }
      73              : 
      74            2 :         std::shared_ptr<Component> Design::GetComponent(const std::string& refDes) const
      75              :         {
      76            2 :                 auto findIt = m_componentsByName.find(refDes);
      77            2 :                 if (findIt != m_componentsByName.end())
      78              :                 {
      79            2 :                         return findIt->second;
      80              :                 }
      81              : 
      82            0 :                 return nullptr;
      83              :         }
      84              : 
      85            2 :         const Part::Vector& Design::GetParts() const
      86              :         {
      87            2 :                 return m_parts;
      88              :         }
      89              : 
      90            2 :         const Part::StringMap& Design::GetPartsByName() const
      91              :         {
      92            2 :                 return m_partsByName;
      93              :         }
      94              : 
      95            0 :         std::shared_ptr<Net> Design::GetNet(const std::string& name) const
      96              :         {
      97            0 :                 auto findIt = m_netsByName.find(name);
      98            0 :                 if (findIt != m_netsByName.end())
      99              :                 {
     100            0 :                         return findIt->second;
     101              :                 }
     102            0 :                 return nullptr;
     103              :         }
     104              : 
     105            0 :         std::shared_ptr<Net> Design::GetNoneNet() const
     106              :         {
     107            0 :                 return GetNet(NONE_NET_NAME);
     108              :         }
     109              : 
     110            2 :         bool Design::Build(std::string path)
     111              :         {
     112            2 :                 auto pFileModel = std::make_shared<FileModel::Design::FileArchive>(path);
     113            2 :                 if (pFileModel->ParseFileModel())
     114              :                 {
     115            2 :                         return Build(pFileModel);
     116              :                 }
     117            0 :                 return false;
     118            2 :         }
     119              : 
     120           58 :         bool Design::Build(std::shared_ptr<FileModel::Design::FileArchive> pFileModel)
     121              :         {               
     122           58 :                 if (pFileModel == nullptr) return false;
     123              : 
     124           58 :                 m_pFileModel = pFileModel;
     125              : 
     126              :                 //m_productModel = m_pFileModel->GetProductName();
     127              :                 //m_name = m_pFileModel->GetProductName();
     128              : 
     129              :                 // atomic elements
     130           58 :                 if (! BuildNets()) return false;
     131           58 :                 if (! BuildPackages()) return false;
     132           58 :                 if (! BuildAllParts()) return false;
     133           58 :                 if (! BuildAllComponents()) return false;
     134              : 
     135              :                 // built from relationships between atomic elements
     136           58 :                 if (! BuildPlacementsFromComponentsFiles()) return false;
     137              : 
     138              :                 // already built from BuildPlacements()
     139              :                 //if (! BuildNoneNet()) return false;
     140              :                 //if (! BreakSinglePinNets()) return false;
     141              : 
     142              :                 if (CLIP_FILEMODEL_AFTER_BUILD)
     143              :                 {
     144              :                         ClipFileModel();
     145              :                 }
     146              : 
     147           58 :                 return true;
     148              :         }
     149              : 
     150           41 :         std::shared_ptr<FileModel::Design::FileArchive> Design::GetFileModel() const
     151              :         {
     152           41 :                 return m_pFileModel;
     153              :         }
     154              : 
     155            1 :         void Design::ClipFileModel()
     156              :         {
     157            1 :                 m_pFileModel = nullptr;
     158            1 :         }
     159              : 
     160            5 :         std::unique_ptr<Odb::Lib::Protobuf::ProductModel::Design> Design::to_protobuf() const
     161              :         {
     162              :                 // Full flavor (with normalized lists) — REST (to_json), to_pbstring,
     163              :                 // to_stream, and round-trip serialization tests all route through this
     164              :                 // override and must continue to see the complete payload.
     165            5 :                 return to_protobuf(/*includeNormalizedLists=*/true);
     166              :         }
     167              : 
     168           78 :         std::unique_ptr<Odb::Lib::Protobuf::ProductModel::Design> Design::to_protobuf(bool includeNormalizedLists) const
     169              :         {
     170           78 :                 auto pDesignMsg = std::make_unique<Odb::Lib::Protobuf::ProductModel::Design>();
     171          156 :                 pDesignMsg->set_name(Odb::Lib::Text::ToUtf8(m_name));
     172          156 :                 pDesignMsg->set_productmodel(Odb::Lib::Text::ToUtf8(m_productModel));
     173              : 
     174           78 :                 if (m_pFileModel != nullptr)
     175              :                 {
     176           76 :                         pDesignMsg->mutable_filemodel()->CopyFrom(*m_pFileModel->to_protobuf());
     177              :                 }
     178              : 
     179           78 :                 if (includeNormalizedLists)
     180              :                 {
     181         5871 :                         for (const auto& pNet : m_nets)
     182              :                         {
     183         5862 :                                 pDesignMsg->add_nets()->CopyFrom(*pNet->to_protobuf());
     184              :                         }
     185              : 
     186            9 :                         FillProtobufMapWithSanitizedKeys(*pDesignMsg->mutable_netsbyname(), m_netsByName);
     187              : 
     188          878 :                         for (const auto& pPackage : m_packages)
     189              :                         {
     190          869 :                                 pDesignMsg->add_packages()->CopyFrom(*pPackage->to_protobuf());
     191              :                         }
     192              : 
     193            9 :                         FillProtobufMapWithSanitizedKeys(*pDesignMsg->mutable_packagesbyname(), m_packagesByName);
     194              : 
     195         7084 :                         for (const auto& pComponent : m_components)
     196              :                         {
     197         7075 :                                 pDesignMsg->add_components()->CopyFrom(*pComponent->to_protobuf());
     198              :                         }
     199              : 
     200            9 :                         FillProtobufMapWithSanitizedKeys(*pDesignMsg->mutable_componentsbyname(), m_componentsByName);
     201              : 
     202         1334 :                         for (const auto& pPart : m_parts)
     203              :                         {
     204         1325 :                                 pDesignMsg->add_parts()->CopyFrom(*pPart->to_protobuf());
     205              :                         }
     206              : 
     207            9 :                         FillProtobufMapWithSanitizedKeys(*pDesignMsg->mutable_partsbyname(), m_partsByName);
     208              :                 }
     209              : 
     210              :                 // Connectivity is always-on (decision D4): derived here
     211              :                 // unconditionally — outside the includeNormalizedLists gate — so every
     212              :                 // flavour carries it and no client ever has to ask for it.
     213           78 :                 FillConnectivityMessage(pDesignMsg->mutable_connectivity());
     214              : 
     215              : #ifndef NDEBUG
     216              :                 // Debug-only assertion: verify all string fields are valid UTF-8 before serialization
     217           78 :                 Odb::Lib::Text::AssertAllStringFieldsAreValidUtf8(*pDesignMsg, "Design");
     218              : #endif
     219              : 
     220           78 :                 return pDesignMsg;
     221            0 :         }
     222              : 
     223           78 :         void Design::FillConnectivityMessage(Protobuf::ProductModel::Connectivity* pConnectivityMsg) const
     224              :         {
     225              :                 // Monolithic path: one message, all four sections. The section
     226              :                 // fillers are the single implementation — the chunked GetDesignStream
     227              :                 // path (M6) calls them with batch-sized Connectivity messages so both
     228              :                 // paths emit byte-identical entries by construction.
     229           78 :                 const auto index = BuildConnectivityIndex();
     230           78 :                 FillConnectivityNetEntries(*pConnectivityMsg);
     231           78 :                 FillConnectivityPackagePins(*pConnectivityMsg);
     232           78 :                 FillConnectivityNetMembers(index, *pConnectivityMsg);
     233           78 :                 FillConnectivityPlacedComponents(index, *pConnectivityMsg);
     234           78 :         }
     235              : 
     236           94 :         Design::ConnectivityIndex Design::BuildConnectivityIndex() const
     237              :         {
     238           94 :                 ConnectivityIndex index;
     239              : 
     240              :                 // $NONE$ ordinal (the roster loop in FillConnectivityNetEntries
     241              :                 // detects it too; the index needs it for the edge filter below).
     242        56683 :                 for (const auto& pNet : m_nets)
     243              :                 {
     244        56589 :                         if (!index.hasNoneNet && pNet->GetName() == NONE_NET_NAME)
     245              :                         {
     246           87 :                                 index.noneNetOrdinal = pNet->GetIndex();
     247           87 :                                 index.hasNoneNet = true;
     248              :                         }
     249              :                 }
     250              : 
     251              :                 // The resolved edges the product model already holds. Grouped by
     252              :                 // refDes then pin number; on a (refDes, pin) that somehow carries two
     253              :                 // edges the last one wins (no fixture design exhibits this).
     254        56683 :                 for (const auto& pNet : m_nets)
     255              :                 {
     256              :                         // $NONE$ membership is a labeling convention, not a connection
     257              :                         // (decision DP4): those pins emit UNCONNECTED and join no
     258              :                         // netMembers entry.
     259        56589 :                         if (index.hasNoneNet && pNet->GetIndex() == index.noneNetOrdinal) continue;
     260              : 
     261       288851 :                         for (const auto& pPinConnection : pNet->GetPinConnections())
     262              :                         {
     263       232349 :                                 const auto& pComponent = pPinConnection->GetComponent();
     264       232349 :                                 const auto& pPin = pPinConnection->GetPin();
     265       232349 :                                 if (pComponent == nullptr || pPin == nullptr) continue;
     266              : 
     267       232349 :                                 index.edgesByRefDes[pComponent->GetRefDes()][pPin->GetIndex()] =
     268       232349 :                                         ConnectivityIndex::Edge{ pNet->GetIndex(), pPinConnection->GetSubnetNumber() };
     269       232349 :                         }
     270              :                 }
     271              : 
     272              :                 // Net members: the same CONNECTED edges, inverted, for the net tab.
     273              :                 // Map iteration order gives net-ordinal-then-refDes determinism and
     274              :                 // pin numbers ascend within each member.
     275        67788 :                 for (const auto& refDesAndPins : index.edgesByRefDes)
     276              :                 {
     277       300043 :                         for (const auto& pinAndEdge : refDesAndPins.second)
     278              :                         {
     279       232349 :                                 index.membersByNet[pinAndEdge.second.net][refDesAndPins.first].push_back(pinAndEdge.first);
     280              :                         }
     281              :                 }
     282           94 :                 return index;
     283            0 :         }
     284              : 
     285           78 :         void Design::FillConnectivityNetEntries(Protobuf::ProductModel::Connectivity& connectivityMsg) const
     286              :         {
     287        48295 :                 for (std::size_t netIndex = 0; netIndex < m_nets.size(); ++netIndex)
     288              :                 {
     289        48217 :                         AddConnectivityNetEntry(netIndex, connectivityMsg);
     290              :                 }
     291           78 :         }
     292              : 
     293           78 :         void Design::FillConnectivityPackagePins(Protobuf::ProductModel::Connectivity& connectivityMsg) const
     294              :         {
     295         7287 :                 for (std::size_t packageIndex = 0; packageIndex < m_packages.size(); ++packageIndex)
     296              :                 {
     297         7209 :                         AddConnectivityPackagePinEntry(packageIndex, connectivityMsg);
     298              :                 }
     299           78 :         }
     300              : 
     301           78 :         void Design::FillConnectivityNetMembers(const ConnectivityIndex& index, Protobuf::ProductModel::Connectivity& connectivityMsg) const
     302              :         {
     303        48221 :                 for (auto membersIt = index.membersByNet.begin(); membersIt != index.membersByNet.end(); ++membersIt)
     304              :                 {
     305        48143 :                         AddConnectivityNetMemberEntry(index, membersIt, connectivityMsg);
     306              :                 }
     307           78 :         }
     308              : 
     309           78 :         void Design::FillConnectivityPlacedComponents(const ConnectivityIndex& index, Protobuf::ProductModel::Connectivity& connectivityMsg) const
     310              :         {
     311        58183 :                 for (std::size_t componentIndex = 0; componentIndex < m_components.size(); ++componentIndex)
     312              :                 {
     313        58105 :                         AddConnectivityPlacedComponentEntry(index, componentIndex, connectivityMsg);
     314              :                 }
     315           78 :         }
     316              : 
     317        56589 :         void Design::AddConnectivityNetEntry(std::size_t netIndex, Protobuf::ProductModel::Connectivity& connectivityMsg) const
     318              :         {
     319              :                 // Net roster: dense — every eda-data NET record in sequence, $NONE$
     320              :                 // included — so `net` ordinals keep their spec identity (p.153: the
     321              :                 // ordinal IS the NET record sequence) and clients need no name
     322              :                 // conventions to interpret them.
     323        56589 :                 const auto& pNet = m_nets[netIndex];
     324        56589 :                 auto pNetEntryMsg = connectivityMsg.add_nets();
     325        56589 :                 pNetEntryMsg->set_ordinal(pNet->GetIndex());
     326       113178 :                 pNetEntryMsg->set_name(Odb::Lib::Text::ToUtf8(pNet->GetName()));
     327        56589 :         }
     328              : 
     329         8132 :         void Design::AddConnectivityPackagePinEntry(std::size_t packageIndex, Protobuf::ProductModel::Connectivity& connectivityMsg) const
     330              :         {
     331              :                 // Package pin rosters — deduped by construction (one entry per
     332              :                 // package, not per component). Package::AddPin assigns sequential
     333              :                 // indices, so each roster is already pin-number ascending.
     334         8132 :                 const auto& pPackage = m_packages[packageIndex];
     335         8132 :                 auto pPackagePinsMsg = connectivityMsg.add_packages();
     336        16264 :                 pPackagePinsMsg->set_package(Odb::Lib::Text::ToUtf8(pPackage->GetName()));
     337       104649 :                 for (const auto& pPin : pPackage->GetPins())
     338              :                 {
     339        96517 :                         auto pPackagePinMsg = pPackagePinsMsg->add_pins();
     340        96517 :                         pPackagePinMsg->set_number(pPin->GetIndex());
     341       193034 :                         pPackagePinMsg->set_name(Odb::Lib::Text::ToUtf8(pPin->GetName()));
     342              :                 }
     343         8132 :         }
     344              : 
     345        56502 :         void Design::AddConnectivityNetMemberEntry(const ConnectivityIndex& index, ConnectivityIndex::MembersByNetConstIt netMembersIt, Protobuf::ProductModel::Connectivity& connectivityMsg) const
     346              :         {
     347        56502 :                 auto pNetMembersMsg = connectivityMsg.add_netmembers();
     348        56502 :                 pNetMembersMsg->set_net(netMembersIt->first);
     349       267908 :                 for (const auto& refDesAndPins : netMembersIt->second)
     350              :                 {
     351       211406 :                         auto pMemberMsg = pNetMembersMsg->add_members();
     352       422812 :                         pMemberMsg->set_refdes(Odb::Lib::Text::ToUtf8(refDesAndPins.first));
     353       443755 :                         for (const auto pinNumber : refDesAndPins.second)
     354              :                         {
     355       232349 :                                 pMemberMsg->add_pin(pinNumber);
     356              :                         }
     357              :                 }
     358        56502 :         }
     359              : 
     360        68674 :         void Design::AddConnectivityPlacedComponentEntry(const ConnectivityIndex& index, std::size_t componentIndex, Protobuf::ProductModel::Connectivity& connectivityMsg) const
     361              :         {
     362              :                 using ConnectivityMsg = Protobuf::ProductModel::Connectivity;
     363              : 
     364              :                 // Placed components in package-roster order (decision DP3). A pin
     365              :                 // with no edge — net_num -1 (skipped at build time) or a roster pin
     366              :                 // the design never placed a toeprint on — is UNCONNECTED with no net
     367              :                 // and no subnets; a toeprintNumber never reaches a client.
     368        68674 :                 const auto& pComponent = m_components[componentIndex];
     369        68674 :                 auto pPlacedComponentMsg = connectivityMsg.add_components();
     370       137348 :                 pPlacedComponentMsg->set_refdes(Odb::Lib::Text::ToUtf8(pComponent->GetRefDes()));
     371        68674 :                 const auto id = pComponent->GetId();
     372        68674 :                 if (id.has_value())
     373              :                 {
     374        68674 :                         pPlacedComponentMsg->set_id(*id);
     375              :                 }
     376        68674 :                 pPlacedComponentMsg->set_side(static_cast<Odb::Lib::Protobuf::BoardSide>(pComponent->GetSide()));
     377              : 
     378        68674 :                 const auto& pPackage = pComponent->GetPackage();
     379        68674 :                 if (pPackage == nullptr) return;
     380       137348 :                 pPlacedComponentMsg->set_package(Odb::Lib::Text::ToUtf8(pPackage->GetName()));
     381              : 
     382        68674 :                 const auto findEdgesIt = index.edgesByRefDes.find(pComponent->GetRefDes());
     383       307043 :                 for (const auto& pPin : pPackage->GetPins())
     384              :                 {
     385       238369 :                         auto pComponentPinMsg = pPlacedComponentMsg->add_pins();
     386       238369 :                         pComponentPinMsg->set_number(pPin->GetIndex());
     387       476738 :                         pComponentPinMsg->set_name(Odb::Lib::Text::ToUtf8(pPin->GetName()));
     388              : 
     389       238369 :                         auto pConnectionMsg = pComponentPinMsg->mutable_connection();
     390       238369 :                         if (findEdgesIt != index.edgesByRefDes.end())
     391              :                         {
     392       237809 :                                 const auto findEdgeIt = findEdgesIt->second.find(pPin->GetIndex());
     393       237809 :                                 if (findEdgeIt != findEdgesIt->second.end())
     394              :                                 {
     395       232349 :                                         pConnectionMsg->set_kind(ConnectivityMsg::Connection::CONNECTED);
     396       232349 :                                         pConnectionMsg->set_net(findEdgeIt->second.net);
     397       232349 :                                         pConnectionMsg->add_subnets(findEdgeIt->second.subnet);
     398       232349 :                                         continue;
     399              :                                 }
     400              :                         }
     401         6020 :                         pConnectionMsg->set_kind(ConnectivityMsg::Connection::UNCONNECTED);
     402              :                 }
     403        68674 :         }
     404              : 
     405            5 :         void Design::from_protobuf(const Odb::Lib::Protobuf::ProductModel::Design& message)
     406              :         {
     407            5 :                 m_name = message.name();
     408            5 :                 m_productModel = message.productmodel();
     409              : 
     410            5 :                 m_pFileModel = std::make_shared<FileModel::Design::FileArchive>();
     411            5 :                 m_pFileModel->from_protobuf(message.filemodel());
     412              : 
     413         1970 :                 for (const auto& pNetMsg : message.nets())
     414              :                 {
     415         1965 :                         auto pNet = std::make_shared<Net>("", -1);
     416         1965 :                         pNet->from_protobuf(pNetMsg);
     417         1965 :                         m_nets.push_back(pNet);
     418         1965 :                 }
     419              : 
     420         3935 :                 for (const auto& kvNetMsg : message.netsbyname())
     421              :                 {
     422         1965 :                         auto pNet = std::make_shared<Net>("", -1);
     423         1965 :                         pNet->from_protobuf(kvNetMsg.second);
     424         1965 :                         m_netsByName[kvNetMsg.first] = pNet;
     425         1965 :                 }
     426              : 
     427          333 :                 for (const auto& pPackageMsg : message.packages())
     428              :                 {
     429          328 :                         auto pPackage = std::make_shared<Package>("", -1);
     430          328 :                         pPackage->from_protobuf(pPackageMsg);
     431          328 :                         m_packages.push_back(pPackage);
     432          328 :                 }
     433              : 
     434          661 :                 for (const auto& kvPackageMsg : message.packagesbyname())
     435              :                 {
     436          328 :                         auto pPackage = std::make_shared<Package>("", -1);
     437          328 :                         pPackage->from_protobuf(kvPackageMsg.second);
     438          328 :                         m_packagesByName[kvPackageMsg.first] = pPackage;
     439          328 :                 }
     440              : 
     441         2323 :                 for (const auto& pComponentMsg : message.components())
     442              :                 {
     443         2318 :                         auto pComponent = std::make_shared<Component>("", "", nullptr, -1, BoardSide::BsNone, nullptr);
     444         2318 :                         pComponent->from_protobuf(pComponentMsg);
     445         2318 :                         m_components.push_back(pComponent);
     446         2318 :                 }
     447              : 
     448         4641 :                 for (const auto& kvComponentMsg : message.componentsbyname())
     449              :                 {
     450         2318 :                         auto pComponent = std::make_shared<Component>("", "", nullptr, -1, BoardSide::BsNone, nullptr);
     451         2318 :                         pComponent->from_protobuf(kvComponentMsg.second);
     452         2318 :                         m_componentsByName[kvComponentMsg.first] = pComponent;
     453         2318 :                 }
     454              : 
     455          453 :                 for (const auto& pPartMsg : message.parts())
     456              :                 {
     457          448 :                         auto pPart = std::make_shared<Part>("");
     458          448 :                         pPart->from_protobuf(pPartMsg);
     459          448 :                         m_parts.push_back(pPart);
     460          448 :                 }
     461              : 
     462          901 :                 for (const auto& kvPartMsg : message.partsbyname())
     463              :                 {
     464          448 :                         auto pPart = std::make_shared<Part>("");
     465          448 :                         pPart->from_protobuf(kvPartMsg.second);
     466          448 :                         m_partsByName[kvPartMsg.first] = pPart;
     467          448 :                 }
     468            5 :         }
     469              : 
     470           82 :         std::uint64_t Design::EstimateWireSizeBytes(bool includeNormalizedLists) const
     471              :         {
     472              :                 // Per-record wire-byte coefficients. These are deliberately rough
     473              :                 // upper-mid estimates, calibrated so that (a) the small CI designs
     474              :                 // (sample_design, rigidflex) stay far below the monolithic serve
     475              :                 // budget and (b) the two OOM designs (200-40628, 350-41017) land far
     476              :                 // above it; the dispatch decision only needs that separation, not an
     477              :                 // accurate size. A miss LOW costs a pod OOM (the exact failure this
     478              :                 // guard exists for); a miss HIGH costs only a streaming detour — so
     479              :                 // the DOMINANT BULK is counted with generous coefficients. Not every
     480              :                 // serialized term is counted per ride: ByName-map duplication of
     481              :                 // net/package/pin records and the per-record propertyrecords embedded
     482              :                 // in NetRecord/PackageRecord/FeatureGroupRecord are amortized into
     483              :                 // the per-record coefficients, and standard-fonts bulk rides the flat
     484              :                 // MISC allowance. DesignStreamTests
     485              :                 // (EstimateWireSizeBytes_StaysAboveHalfOfActualSerializedSize) pins
     486              :                 // estimate >= 0.5x actual on the CI corpus so a dropped term cannot
     487              :                 // pass CI silently.
     488              :                 // A FeatureRecord carries ~10 scalar fields plus occasional
     489              :                 // strings/contours (~128 B), a component rides three places
     490              :                 // (components file + product-model Component + connectivity
     491              :                 // PlacedComponent with per-pin entries, ~768 B), a net two-to-three
     492              :                 // places (~128 B), a package record with its pin roster ~256 B.
     493           82 :                 constexpr std::uint64_t FEATURE_RECORD_WIRE_BYTES = 128;
     494           82 :                 constexpr std::uint64_t COMPONENT_WIRE_BYTES = 768;
     495           82 :                 constexpr std::uint64_t NET_WIRE_BYTES = 128;
     496           82 :                 constexpr std::uint64_t PACKAGE_WIRE_BYTES = 256;
     497              :                 // Subnet feature-id: type+layer+feature varints (~8 B wire), riding
     498              :                 // twice (EdaDataFile netRecords + netRecordsByName map).
     499           82 :                 constexpr std::uint64_t FEATURE_ID_RECORD_WIRE_BYTES = 16;
     500              :                 // Netlist-file records: serial#+netName (net) / ~13 numeric fields
     501              :                 // (point); both ride twice (records + ByName map).
     502           82 :                 constexpr std::uint64_t NETLIST_NET_WIRE_BYTES = 64;
     503           82 :                 constexpr std::uint64_t NETLIST_POINT_WIRE_BYTES = 64;
     504              :                 // Attribute-string entries (attrlist files, eda layer/attribute/text
     505              :                 // tables, per-record attribute lookup tables): name + value strings.
     506           82 :                 constexpr std::uint64_t ATTRIBUTE_ENTRY_WIRE_BYTES = 64;
     507              :                 // EdaDataFile property + feature-group records (name + value refs).
     508           82 :                 constexpr std::uint64_t PROPERTY_RECORD_WIRE_BYTES = 64;
     509              :                 // Tools-file drill/tool records (type, diameter, plus params).
     510           82 :                 constexpr std::uint64_t TOOL_RECORD_WIRE_BYTES = 64;
     511              :                 // Normalized flavour: a Part entry (name + package ref) rides the
     512              :                 // parts vector and the partsByName map.
     513           82 :                 constexpr std::uint64_t PART_WIRE_BYTES = 128;
     514              :                 // Header-ish files (matrix, misc/info, step headers, standard fonts)
     515              :                 // hold bounded structural payloads, not per-record bulk: a flat
     516              :                 // allowance per design plus per step covers them with margin.
     517           82 :                 constexpr std::uint64_t MISC_DESIGN_ALLOWANCE_BYTES = 256 * 1024;
     518           82 :                 constexpr std::uint64_t MISC_STEP_ALLOWANCE_BYTES = 64 * 1024;
     519              : 
     520           82 :                 std::uint64_t featureCount = 0;
     521           82 :                 std::uint64_t componentCount = 0;
     522           82 :                 std::uint64_t netCount = 0;
     523           82 :                 std::uint64_t packageCount = 0;
     524           82 :                 std::uint64_t featureIdCount = 0;
     525           82 :                 std::uint64_t netlistNetCount = 0;
     526           82 :                 std::uint64_t netlistPointCount = 0;
     527           82 :                 std::uint64_t attributeEntryCount = 0;
     528           82 :                 std::uint64_t propertyRecordCount = 0;
     529           82 :                 std::uint64_t toolCount = 0;
     530           82 :                 std::uint64_t stepCount = 0;
     531              : 
     532           82 :                 if (m_pFileModel != nullptr)
     533              :                 {
     534           82 :                         attributeEntryCount += m_pFileModel->GetMiscAttrListFile().GetAttributes().size();
     535              : 
     536         4558 :                         for (const auto& kvSymbolsDirectory : m_pFileModel->GetSymbolsDirectoriesByName())
     537              :                         {
     538         4476 :                                 const auto& pSymbolsDirectory = kvSymbolsDirectory.second;
     539         4476 :                                 featureCount += pSymbolsDirectory->GetFeaturesFile().GetFeatureRecords().size();
     540         4476 :                                 attributeEntryCount += pSymbolsDirectory->GetAttrListFile().GetAttributes().size();
     541              :                         }
     542              : 
     543          164 :                         for (const auto& kvStep : m_pFileModel->GetStepsByName())
     544              :                         {
     545           82 :                                 const auto& pStep = kvStep.second;
     546           82 :                                 ++stepCount;
     547           82 :                                 featureCount += pStep->GetProfileFile().GetFeatureRecords().size();
     548           82 :                                 attributeEntryCount += pStep->GetAttrListFile().GetAttributes().size();
     549              : 
     550           82 :                                 const auto& edaDataFile = pStep->GetEdaDataFile();
     551           82 :                                 netCount += edaDataFile.GetNetRecords().size();
     552           82 :                                 packageCount += edaDataFile.GetPackageRecords().size();
     553           82 :                                 attributeEntryCount += edaDataFile.GetLayerNames().size()
     554           82 :                                         + edaDataFile.GetAttributeNames().size()
     555           82 :                                         + edaDataFile.GetAttributeTextValues().size();
     556           82 :                                 propertyRecordCount += edaDataFile.GetPropertyRecords().size()
     557           82 :                                         + edaDataFile.GetFeatureGroupRecords().size();
     558        53418 :                                 for (const auto& pNetRecord : edaDataFile.GetNetRecords())
     559              :                                 {
     560       851078 :                                         for (const auto& pSubnetRecord : pNetRecord->m_subnetRecords)
     561              :                                         {
     562       797742 :                                                 featureIdCount += pSubnetRecord->m_featureIdRecords.size();
     563              :                                         }
     564        53336 :                                         attributeEntryCount += pNetRecord->GetAttributeLookupTable().size();
     565              :                                 }
     566         7744 :                                 for (const auto& pPackageRecord : edaDataFile.GetPackageRecords())
     567              :                                 {
     568         7662 :                                         attributeEntryCount += pPackageRecord->GetAttributeLookupTable().size();
     569              :                                 }
     570              : 
     571          164 :                                 for (const auto& kvNetlist : pStep->GetNetlistsByName())
     572              :                                 {
     573           82 :                                         netlistNetCount += kvNetlist.second->GetNetRecords().size();
     574           82 :                                         netlistPointCount += kvNetlist.second->GetNetPointRecords().size();
     575              :                                 }
     576              : 
     577         2486 :                                 for (const auto& kvLayer : pStep->GetLayersByName())
     578              :                                 {
     579         2404 :                                         const auto& pLayer = kvLayer.second;
     580         2404 :                                         featureCount += pLayer->GetFeaturesFile().GetFeatureRecords().size();
     581         2404 :                                         componentCount += pLayer->GetComponentsFile().GetComponentRecords().size();
     582         2404 :                                         attributeEntryCount += pLayer->GetAttrListFile().GetAttributes().size();
     583         2404 :                                         toolCount += pLayer->GetToolsFile().GetTools().size();
     584              :                                 }
     585              :                         }
     586              :                 }
     587              : 
     588           82 :                 auto estimate = featureCount * FEATURE_RECORD_WIRE_BYTES
     589           82 :                         + componentCount * COMPONENT_WIRE_BYTES
     590           82 :                         + netCount * NET_WIRE_BYTES
     591           82 :                         + packageCount * PACKAGE_WIRE_BYTES
     592              :                         + featureIdCount * FEATURE_ID_RECORD_WIRE_BYTES
     593           82 :                         + netlistNetCount * NETLIST_NET_WIRE_BYTES
     594           82 :                         + netlistPointCount * NETLIST_POINT_WIRE_BYTES
     595           82 :                         + attributeEntryCount * ATTRIBUTE_ENTRY_WIRE_BYTES
     596           82 :                         + propertyRecordCount * PROPERTY_RECORD_WIRE_BYTES
     597           82 :                         + toolCount * TOOL_RECORD_WIRE_BYTES
     598           82 :                         + MISC_DESIGN_ALLOWANCE_BYTES + stepCount * MISC_STEP_ALLOWANCE_BYTES;
     599              : 
     600           82 :                 if (includeNormalizedLists)
     601              :                 {
     602              :                         // The full flavour repeats nets/components/packages (vectors plus
     603              :                         // ByName maps) on top of the fileModel-derived payload, and adds
     604              :                         // the parts vector + partsByName map.
     605            3 :                         estimate += netCount * NET_WIRE_BYTES
     606              :                                 + componentCount * COMPONENT_WIRE_BYTES
     607            3 :                                 + packageCount * PACKAGE_WIRE_BYTES
     608            3 :                                 + m_parts.size() * PART_WIRE_BYTES;
     609              :                 }
     610              : 
     611           82 :                 return estimate;
     612              :         }
     613              : 
     614           58 :         bool Design::BuildAllComponents()
     615              :         {
     616           58 :                 if (m_pFileModel == nullptr) return false;
     617           58 :                 const auto& steps = m_pFileModel->GetStepsByName();
     618           58 :                 if (steps.empty()) return false;
     619           58 :                 auto& pStepDirectory = steps.begin()->second;
     620              : 
     621              :                 // top components layer
     622           58 :                 auto pTopComponentsFile = pStepDirectory->GetTopComponentsFile();
     623           58 :                 if (pTopComponentsFile == nullptr) return false;
     624           58 :                 if (! BuildComponents(pTopComponentsFile)) return false;
     625              : 
     626              :                 // bottom layer components
     627           58 :                 auto pBottomComponentsFile = pStepDirectory->GetBottomComponentsFile();
     628           58 :                 if (pBottomComponentsFile == nullptr) return false;
     629           58 :                 if (!BuildComponents(pBottomComponentsFile)) return false;
     630              : 
     631           58 :                 return true;
     632              :         }
     633              : 
     634          116 :         bool Design::BuildComponents(const Odb::Lib::FileModel::Design::ComponentsFile* pComponentsFile)
     635              :         {
     636          116 :                 const auto& componentRecords = pComponentsFile->GetComponentRecords();
     637        45334 :                 for (const auto& pComponentRecord : componentRecords)
     638              :                 {
     639        45218 :                         auto& pPackage = m_packages[pComponentRecord->pkgRef];
     640        45218 :                         auto index = pComponentRecord->index;
     641        45218 :                         auto& pPart = m_partsByName[pComponentRecord->partName];
     642        45218 :                         auto pComponent = std::make_shared<Component>(pComponentRecord->compName, pComponentRecord->partName, pPackage, index, pComponentsFile->GetSide(), pPart, pComponentRecord->id);
     643              : 
     644        45218 :                         m_components.push_back(pComponent);
     645        45218 :                         m_componentsByName[pComponent->GetRefDes()] = pComponent;
     646        45218 :                 }
     647              : 
     648          116 :                 return true;
     649              :         }
     650              : 
     651            0 :         bool Design::BuildVias()
     652              :         {
     653            0 :                 return false;
     654              :         }
     655              : 
     656           58 :         bool Design::BuildNets()
     657              :         {               
     658           58 :                 if (m_pFileModel == nullptr) return false;
     659              : 
     660           58 :                 const auto& steps = m_pFileModel->GetStepsByName();
     661           58 :                 if (steps.empty()) return false;
     662              : 
     663           58 :                 auto& pStepDirectory = steps.begin()->second;
     664           58 :                 const auto& edaData = pStepDirectory->GetEdaDataFile();
     665           58 :                 const auto& netRecords = edaData.GetNetRecords();
     666              : 
     667        37938 :                 for (const auto& pNetRecord : netRecords)
     668              :                 {
     669        37880 :                         auto pNet = std::make_shared<Net>(pNetRecord->name, pNetRecord->index);
     670        37880 :                         m_nets.push_back(pNet);
     671        37880 :                         m_netsByName[pNet->GetName()] = pNet;
     672        37880 :                 }       
     673              : 
     674           58 :                 return true;
     675              :         }
     676              : 
     677           58 :         bool Design::BuildPackages()
     678              :         {
     679           58 :                 if (m_pFileModel == nullptr) return false;
     680              : 
     681           58 :                 const auto& steps = m_pFileModel->GetStepsByName();
     682           58 :                 if (steps.empty()) return false;
     683              :                 
     684           58 :                 auto& pStepDirectory = steps.begin()->second;
     685           58 :                 const auto& edaData = pStepDirectory->GetEdaDataFile();
     686           58 :                 const auto& packageRecords = edaData.GetPackageRecords();
     687              : 
     688         6016 :                 for (const auto& pPackageRecord : packageRecords)
     689              :                 {
     690         5958 :                         auto index = pPackageRecord->index;
     691         5958 :                         auto pPackage = std::make_shared<Package>(pPackageRecord->name, index);
     692              : 
     693        72748 :                         for (const auto& pPinRecord : pPackageRecord->m_pinRecords)
     694              :                         {                               
     695              :                                 // TODO: figure out how to handle size_t -> non-size_t conversion
     696        66790 :                                 pPackage->AddPin(pPinRecord->name);
     697              :                         }
     698              : 
     699         5958 :                         m_packages.push_back(pPackage);
     700         5958 :                         m_packagesByName[pPackage->GetName()] = pPackage;
     701         5958 :                 }               
     702              : 
     703           58 :                 return true;
     704              :         }
     705              : 
     706           58 :         bool Design::BuildAllParts()
     707              :         {
     708           58 :                 if (m_pFileModel == nullptr) return false;
     709              : 
     710           58 :                 const auto& steps = m_pFileModel->GetStepsByName();
     711           58 :                 if (steps.empty()) return false;
     712              : 
     713           58 :                 auto& pStepDirectory = steps.begin()->second;
     714              : 
     715           58 :                 auto pTopComponentsFile = pStepDirectory->GetTopComponentsFile();
     716           58 :                 if (pTopComponentsFile == nullptr) return false;
     717           58 :                 if (! BuildParts(pTopComponentsFile)) return false;
     718              : 
     719           58 :                 auto pBottomComponentsFile = pStepDirectory->GetBottomComponentsFile();
     720           58 :                 if (pBottomComponentsFile == nullptr) return false;
     721           58 :                 if (! BuildParts(pBottomComponentsFile)) return false;
     722              : 
     723           58 :                 return true;
     724              :         }
     725              : 
     726          116 :         bool Design::BuildParts(const Odb::Lib::FileModel::Design::ComponentsFile* pComponentsFile)
     727              :         {
     728          116 :                 const auto& componentRecords = pComponentsFile->GetComponentRecords();
     729        45334 :                 for (const auto& pComponentRecord : componentRecords)
     730              :                 {
     731        45218 :                         auto& partName = pComponentRecord->partName;
     732        45218 :                         auto findIt = m_partsByName.find(partName);
     733        45218 :                         if (findIt == m_partsByName.end())
     734              :                         {
     735         8598 :                                 auto pPart = std::make_shared<Part>(partName);
     736         8598 :                                 m_parts.push_back(pPart);
     737         8598 :                                 m_partsByName[partName] = pPart;
     738         8598 :                         }
     739              :                 }
     740              : 
     741          116 :                 return true;
     742              :         }
     743              : 
     744           58 :         bool Design::BuildPlacementsFromComponentsFiles()
     745              :         {
     746           58 :                 if (m_pFileModel == nullptr) return false;
     747              : 
     748           58 :                 const auto& steps = m_pFileModel->GetStepsByName();
     749           58 :                 if (steps.empty()) return false;
     750              : 
     751              :                 // "first" step when there are >1 steps
     752           58 :                 auto& pStepDirectory = steps.begin()->second;
     753              : 
     754           58 :                 auto pTopComponentsFile = pStepDirectory->GetTopComponentsFile();
     755           58 :                 if (pTopComponentsFile == nullptr) return false;
     756           58 :                 if (! BuildPlacementsFromComponentsFile(pTopComponentsFile)) return false;
     757              : 
     758           58 :                 auto pBottomComponentsFile = pStepDirectory->GetBottomComponentsFile();
     759           58 :                 if (pBottomComponentsFile == nullptr) return false;
     760           58 :                 if (!BuildPlacementsFromComponentsFile(pBottomComponentsFile)) return false;
     761              : 
     762           58 :                 return true;
     763              :         }
     764              : 
     765          116 :         bool Design::BuildPlacementsFromComponentsFile(const Odb::Lib::FileModel::Design::ComponentsFile* pComponentsFile)
     766              :         {
     767          116 :                 const auto& componentRecords = pComponentsFile->GetComponentRecords();
     768        45334 :                 for (const auto& pComponentRecord : componentRecords)
     769              :                 {
     770        45218 :                         const auto& toeprintRecords = pComponentRecord->m_toeprintRecords;
     771       202432 :                         for (const auto& pToeprintRecord : toeprintRecords)
     772              :                         {                               
     773       157214 :                         auto& toeprintName = pToeprintRecord->name;
     774       157214 :                         auto pinNumber = pToeprintRecord->pinNumber;
     775       157214 :                         auto netNumber = pToeprintRecord->netNumber;
     776       157214 :                         auto subnetNumber = pToeprintRecord->subnetNumber;
     777       157214 :                         auto& refDes = pComponentRecord->compName;
     778              : 
     779              :                         // -1 means no connection for the component pin
     780       157214 :                         if (netNumber != static_cast<unsigned int>(-1))
     781              :                         {
     782       157214 :                                 if (!CreatePinConnection(refDes, netNumber, pinNumber, toeprintName, subnetNumber)) return false;
     783              :                         }
     784              :                         }
     785              :                 }
     786              : 
     787          116 :                 return true;
     788              :         }
     789              : 
     790       157214 :         bool Design::CreatePinConnection(const std::string& refDes, unsigned int netNumber, unsigned int pinNumber, const std::string& pinName, unsigned int subnetNumber)
     791              :         {
     792       157214 :                 if (netNumber < m_nets.size())
     793              :                 {
     794       157214 :                         auto& pComponent = m_componentsByName[refDes];
     795       157214 :                         if (pComponent == nullptr) return false;
     796              : 
     797       157214 :                         auto pPin = pComponent->GetPackage()->GetPin(pinNumber);
     798       157214 :                         if (pPin == nullptr) return false;
     799              : 
     800       157214 :                         auto& pNet = m_nets[netNumber];
     801       157214 :                         if (pNet == nullptr) return false;
     802              : 
     803       157214 :                         if (!pNet->AddPinConnection(pComponent, pPin, subnetNumber)) return false;
     804       157214 :                         return true;
     805       157214 :                 }
     806              :                 else
     807              :                 {
     808            0 :                         logwarn("netNumber out of range: " + std::to_string(netNumber) + ", size = " + std::to_string(m_nets.size()));
     809              :                 }       
     810              : 
     811            0 :                 return false;
     812              :         }
     813              : 
     814            0 :         bool Design::BuildNoneNet()
     815              :         {
     816            0 :                 auto pStepDirectory = m_pFileModel->GetStepDirectory();
     817            0 :                 if (pStepDirectory == nullptr) return false;            
     818              : 
     819            0 :                 const auto& edaData = pStepDirectory->GetEdaDataFile();
     820            0 :                 auto findIt = edaData.GetNetRecordsByName().find(NONE_NET_NAME);
     821            0 :                 if (findIt == edaData.GetNetRecordsByName().end()) return false;
     822              :                 
     823            0 :                 auto& pNoneNet = findIt->second;
     824            0 :                 if (pNoneNet == nullptr) return false;
     825              : 
     826            0 :                 if (!CreateNetConnections(pNoneNet, pStepDirectory)) return false;
     827              : 
     828            0 :                 return true;
     829            0 :         }
     830              : 
     831            0 :         bool Design::BreakSinglePinNets()
     832              :         {
     833            0 :                 const auto& pNoneNet = GetNoneNet();
     834            0 :                 if (pNoneNet == nullptr) return false;
     835              : 
     836            0 :                 auto& pinConnections = pNoneNet->GetPinConnections();            
     837            0 :                 while (!pinConnections.empty())
     838              :                 {
     839            0 :                         const auto& pExistingPinConnection = pinConnections.back();                         
     840              :                         
     841            0 :                         std::string newNetName = "$NC_" + pExistingPinConnection->GetComponent()->GetRefDes() + "_" + pExistingPinConnection->GetPin()->GetName();
     842            0 :                         auto pNewNet = std::make_shared<Net>(newNetName, -1);
     843            0 :                         pNewNet->AddPinConnection(pExistingPinConnection->GetComponent(), pExistingPinConnection->GetPin());
     844            0 :                         m_nets.push_back(pNewNet);
     845            0 :                         m_netsByName[pNewNet->GetName()] = pNewNet;
     846              : 
     847            0 :                         pinConnections.pop_back();                                              
     848            0 :                 }                       
     849              : 
     850            0 :                 return true;
     851            0 :         }
     852              : 
     853              : 
     854            0 :         bool Design::CreateNetConnections(const std::shared_ptr<Odb::Lib::FileModel::Design::EdaDataFile::NetRecord>& pNetRecord, const std::shared_ptr<FileModel::Design::StepDirectory>& pStepDirectory)
     855              :         {
     856            0 :                 for (const auto& pSubnetRecord : pNetRecord->m_subnetRecords)
     857              :                 {
     858            0 :                         if (pSubnetRecord->type == FileModel::Design::EdaDataFile::NetRecord::SubnetRecord::Type::Toeprint)
     859              :                         {                                                                               
     860            0 :                                 const FileModel::Design::ComponentsFile* pComponentsFileToUse = nullptr;
     861              : 
     862            0 :                                 auto side = pSubnetRecord->side;
     863            0 :                                 if (side == BoardSide::Top)
     864              :                                 {
     865            0 :                                         pComponentsFileToUse = pStepDirectory->GetTopComponentsFile();
     866              :                                 }
     867              :                                 else //if (side == BoardSide::Bottom)
     868              :                                 {
     869            0 :                                         pComponentsFileToUse = pStepDirectory->GetBottomComponentsFile();
     870              :                                 }
     871              : 
     872            0 :                                 if (pComponentsFileToUse == nullptr) return false;
     873              : 
     874            0 :                                 auto componentNumber = pSubnetRecord->componentNumber;                               
     875            0 :                                 if (componentNumber >= pComponentsFileToUse->GetComponentRecords().size()) return false;
     876              : 
     877            0 :                                 auto& pComponentRecord = pComponentsFileToUse->GetComponentRecords()[componentNumber];
     878            0 :                                 if (pComponentRecord == nullptr) return false;
     879              : 
     880            0 :                                 auto toeprintNumber = pSubnetRecord->toeprintNumber;
     881            0 :                                 if (toeprintNumber >= pComponentRecord->m_toeprintRecords.size()) return false;
     882              :                                 
     883            0 :                                 auto& pToeprintRecord = pComponentRecord->m_toeprintRecords[toeprintNumber];
     884            0 :                                 if (pToeprintRecord == nullptr) return false;
     885              : 
     886            0 :                                 auto& toeprintName = pToeprintRecord->name;
     887            0 :                                 auto pinNumber = pToeprintRecord->pinNumber;
     888            0 :                                 auto netNumber = pToeprintRecord->netNumber;
     889            0 :                                 auto& refDes = pComponentRecord->compName;
     890              :                                 //auto subnetNumber = pToeprintRecord->subnetNumber;
     891              : 
     892            0 :                                 if (!CreatePinConnection(refDes, netNumber, pinNumber, toeprintName)) return false;                     
     893              :                         }
     894            0 :                         else if (pSubnetRecord->type == FileModel::Design::EdaDataFile::NetRecord::SubnetRecord::Type::Via)
     895              :                         {
     896              :                                 // ?
     897              :                         }
     898              :                 }
     899              : 
     900            0 :                 return true;
     901              :         }
     902              : 
     903              : } // namespace Odb::Lib::ProductModel
        

Generated by: LCOV version 2.0-1