2 * Copyright (c) 2007-2015, SUSE LLC
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Transaction ordering
20 #include "transaction.h"
26 struct s_TransactionElement {
27 Id p; /* solvable id */
28 Id edges; /* pointer into edges data */
32 struct s_TransactionOrderdata {
33 struct s_TransactionElement *tes;
40 #define TYPE_BROKEN (1<<0)
41 #define TYPE_CON (1<<1)
43 #define TYPE_REQ_P (1<<2)
44 #define TYPE_PREREQ_P (1<<3)
46 #define TYPE_SUG (1<<4)
47 #define TYPE_REC (1<<5)
49 #define TYPE_REQ (1<<6)
50 #define TYPE_PREREQ (1<<7)
52 #define TYPE_CYCLETAIL (1<<16)
53 #define TYPE_CYCLEHEAD (1<<17)
55 #define EDGEDATA_BLOCK 127
58 transaction_clone_orderdata(Transaction *trans, Transaction *srctrans)
60 struct s_TransactionOrderdata *od = srctrans->orderdata;
63 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
64 trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
65 trans->orderdata->ntes = od->ntes;
66 trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
67 trans->orderdata->ninvedgedata = od->ninvedgedata;
70 trans->orderdata->cycles = solv_calloc(1, sizeof(Queue));
71 queue_init_clone(trans->orderdata->cycles, od->cycles);
76 transaction_free_orderdata(Transaction *trans)
80 struct s_TransactionOrderdata *od = trans->orderdata;
81 od->tes = solv_free(od->tes);
82 od->invedgedata = solv_free(od->invedgedata);
85 queue_free(od->cycles);
86 od->cycles = solv_free(od->cycles);
88 trans->orderdata = solv_free(trans->orderdata);
94 struct s_TransactionElement *tes;
106 addteedge(struct orderdata *od, int from, int to, int type)
109 struct s_TransactionElement *te;
114 /* printf("edge %d(%s) -> %d(%s) type %x\n", from, pool_solvid2str(pool, od->tes[from].p), to, pool_solvid2str(pool, od->tes[to].p), type); */
117 for (i = te->edges; od->edgedata[i]; i += 2)
118 if (od->edgedata[i] == to)
120 /* test of brokenness */
121 if (type == TYPE_BROKEN)
122 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
125 od->edgedata[i + 1] |= type;
128 if (i + 1 == od->nedgedata)
130 /* printf("tail add %d\n", i - te->edges); */
133 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
137 /* printf("extend %d\n", i - te->edges); */
138 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
140 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
141 i = od->nedgedata + (i - te->edges);
142 te->edges = od->nedgedata;
144 od->edgedata[i] = to;
145 od->edgedata[i + 1] = type;
146 od->edgedata[i + 2] = 0; /* end marker */
147 od->nedgedata = i + 3;
152 addedge(struct orderdata *od, Id from, Id to, int type)
154 Transaction *trans = od->trans;
155 Pool *pool = trans->pool;
157 struct s_TransactionElement *te;
160 /* printf("addedge %d %d type %d\n", from, to, type); */
161 s = pool->solvables + from;
162 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
164 /* obsolete, map to install */
165 if (trans->transaction_installed[from - pool->installed->start] > 0)
166 from = trans->transaction_installed[from - pool->installed->start];
173 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
174 transaction_all_obs_pkgs(trans, from, &ti);
175 for (i = 0; i < ti.count; i++)
176 ret |= addedge(od, ti.elements[i], to, type);
181 s = pool->solvables + to;
182 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
184 /* obsolete, map to install */
185 if (trans->transaction_installed[to - pool->installed->start] > 0)
186 to = trans->transaction_installed[to - pool->installed->start];
193 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
194 transaction_all_obs_pkgs(trans, to, &ti);
195 for (i = 0; i < ti.count; i++)
196 ret |= addedge(od, from, ti.elements[i], type);
202 /* map from/to to te numbers */
203 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
210 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
216 return addteedge(od, i, to, type);
220 havescripts(Pool *pool, Id solvid)
222 Solvable *s = pool->solvables + solvid;
228 reqp = s->repo->idarraydata + s->requires;
229 while ((req = *reqp++) != 0)
231 if (req == SOLVABLE_PREREQMARKER)
238 dep = pool_id2str(pool, req);
239 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
247 addsolvableedges(struct orderdata *od, Solvable *s)
249 Transaction *trans = od->trans;
250 Pool *pool = trans->pool;
252 int i, j, pre, numins;
253 Repo *installed = pool->installed;
259 printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
261 p = s - pool->solvables;
266 reqp = s->repo->idarraydata + s->requires;
268 while ((req = *reqp++) != 0)
270 if (req == SOLVABLE_PREREQMARKER)
276 numins = 0; /* number of packages to be installed providing it */
277 provbyinst = 0; /* provided by kept package */
278 FOR_PROVIDES(p2, pp2, req)
280 s2 = pool->solvables + p2;
283 depq.count = 0; /* self provides */
286 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
291 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
292 continue; /* package stays uninstalled */
294 if (s->repo == installed)
296 /* s gets uninstalled */
297 queue_pushunique(&depq, p2);
298 if (s2->repo != installed)
303 if (s2->repo == installed)
304 continue; /* s2 gets uninstalled */
305 queue_pushunique(&depq, p2);
310 /* prune to harmless ->inst edges */
311 for (i = j = 0; i < depq.count; i++)
312 if (pool->solvables[depq.elements[i]].repo != installed)
313 depq.elements[j++] = depq.elements[i];
317 if (numins && depq.count)
319 if (s->repo == installed)
321 for (i = 0; i < depq.count; i++)
323 if (pool->solvables[depq.elements[i]].repo == installed)
325 for (j = 0; j < depq.count; j++)
327 if (pool->solvables[depq.elements[j]].repo != installed)
329 if (trans->transaction_installed[depq.elements[i] - pool->installed->start] == depq.elements[j])
330 continue; /* no self edge */
332 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, depq.elements[i]), pool_dep2str(pool, req), pool_solvid2str(pool, depq.elements[j]));
334 addedge(od, depq.elements[i], depq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
340 /* no mixed types, remove all deps on uninstalls */
341 for (i = j = 0; i < depq.count; i++)
342 if (pool->solvables[depq.elements[i]].repo != installed)
343 depq.elements[j++] = depq.elements[i];
346 for (i = 0; i < depq.count; i++)
348 p2 = depq.elements[i];
349 if (pool->solvables[p2].repo != installed)
351 /* all elements of depq are installs, thus have different TEs */
352 if (pool->solvables[p].repo != installed)
355 printf("add inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
357 addedge(od, p, p2, pre);
362 printf("add uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
364 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
369 if (s->repo != installed)
370 continue; /* no inst->uninst edges, please! */
372 /* uninst -> uninst edge. Those make trouble. Only add if we must */
373 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
375 /* p is obsoleted by another package and has no scripts */
376 /* we assume that the obsoletor is good enough to replace p */
380 printf("add uninst->uninst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
382 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
390 conp = s->repo->idarraydata + s->conflicts;
391 while ((con = *conp++) != 0)
393 FOR_PROVIDES(p2, pp2, con)
397 s2 = pool->solvables + p2;
400 if (s->repo == installed)
402 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
404 /* deinstall p before installing p2 */
406 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
408 addedge(od, p2, p, TYPE_CON);
413 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
415 /* deinstall p2 before installing p */
417 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
419 addedge(od, p, p2, TYPE_CON);
426 if (s->recommends && s->repo != installed)
429 recp = s->repo->idarraydata + s->recommends;
430 while ((rec = *recp++) != 0)
433 FOR_PROVIDES(p2, pp2, rec)
435 s2 = pool->solvables + p2;
438 depq.count = 0; /* self provides */
441 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
443 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
444 continue; /* package stays uninstalled */
445 if (s2->repo != installed)
446 queue_pushunique(&depq, p2);
448 for (i = 0; i < depq.count; i++)
450 p2 = depq.elements[i];
451 if (pool->solvables[p2].repo != installed)
454 printf("add recommends inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, rec), pool_solvid2str(pool, p2));
456 addedge(od, p, p2, TYPE_REC);
461 if (s->suggests && s->repo != installed)
464 sugp = s->repo->idarraydata + s->suggests;
465 while ((sug = *sugp++) != 0)
468 FOR_PROVIDES(p2, pp2, sug)
470 s2 = pool->solvables + p2;
473 depq.count = 0; /* self provides */
476 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
478 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
479 continue; /* package stays uninstalled */
480 if (s2->repo != installed)
481 queue_pushunique(&depq, p2);
483 for (i = 0; i < depq.count; i++)
485 p2 = depq.elements[i];
486 if (pool->solvables[p2].repo != installed)
489 printf("add suggests inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, sug), pool_solvid2str(pool, p2));
491 addedge(od, p, p2, TYPE_SUG);
496 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &depq) && depq.count)
498 /* we're getting deinstalled/updated. Try to do this before our
499 * triggers are hit */
500 for (i = 0; i < depq.count; i++)
502 Id tri = depq.elements[i];
503 FOR_PROVIDES(p2, pp2, tri)
507 s2 = pool->solvables + p2;
510 if (s2->name == s->name)
511 continue; /* obsoleted anyway */
512 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
514 /* deinstall/update p before installing p2 */
516 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
518 addedge(od, p2, p, TYPE_CON);
527 /* break an edge in a cycle */
529 breakcycle(struct orderdata *od, Id *cycle)
531 Pool *pool = od->trans->pool;
532 Id ddegmin, ddegmax, ddeg;
534 struct s_TransactionElement *te;
537 ddegmin = ddegmax = 0;
538 for (k = 0; cycle[k + 1]; k += 2)
540 ddeg = od->edgedata[cycle[k + 1] + 1];
543 if (!k || ddeg < ddegmin)
551 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
553 /* prefer k, as l comes from a package with contains scriptlets */
557 /* same edge value, check for prereq */
561 /* record brkoen cycle starting with the tail */
562 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
563 queue_push(&od->cycles, k / 2); /* cycle elements */
564 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
565 queue_push(&od->cycles, (ddegmax << 16) | ddegmin); /* max/min values */
572 queue_push(&od->cyclesdata, cycle[k]);
576 queue_push(&od->cyclesdata, 0); /* mark end */
578 /* break that edge */
579 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
582 if (ddegmin < TYPE_REQ)
586 /* cycle recorded, print it */
587 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
588 POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
589 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
590 for (k = 0; cycle[k + 1]; k += 2)
592 te = od->tes + cycle[k];
593 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
594 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
596 POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
598 POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
602 dump_tes(struct orderdata *od)
604 Pool *pool = od->trans->pool;
607 struct s_TransactionElement *te, *te2;
610 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
612 Solvable *s = pool->solvables + te->p;
613 POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
614 if (s->repo != pool->installed)
617 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
618 for (j = 0; j < obsq.count; j++)
619 POOL_DEBUG(SOLV_DEBUG_RESULT, " -%s\n", pool_solvid2str(pool, obsq.elements[j]));
621 for (j = te->edges; od->edgedata[j]; j += 2)
623 te2 = od->tes + od->edgedata[j];
624 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
625 POOL_DEBUG(SOLV_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
627 POOL_DEBUG(SOLV_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
633 reachable(struct orderdata *od, Id i)
635 struct s_TransactionElement *te = od->tes + i;
641 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
643 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
645 if (!od->tes[k].mark)
647 if (od->tes[k].mark == 2)
657 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
660 Transaction *trans = od->trans;
661 Pool *pool = trans->pool;
663 struct s_TransactionElement *te;
668 printf("addcycleedges\n");
669 for (i = 0; (j = cycle[i]) != 0; i++)
670 printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
673 /* first add all the tail cycle edges */
675 /* see what we can reach from the cycle */
677 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
679 for (i = 0; (j = cycle[i]) != 0; i++)
681 od->tes[j].mark = -1;
690 te->mark = te->mark < 0 ? 2 : 1;
691 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
693 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
695 if (od->tes[k].mark > 0)
696 continue; /* no need to visit again */
700 /* now all cycle TEs are marked with 2, all TEs reachable
701 * from the cycle are marked with 1 */
703 od->tes[tail].mark = 1; /* no need to add edges */
705 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
708 continue; /* reachable from cycle */
709 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
711 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
713 if (od->tes[k].mark != 2)
715 /* We found an edge to the cycle. Add an extra edge to the tail */
716 /* the TE was not reachable, so we're not creating a new cycle! */
718 printf("adding TO TAIL cycle edge %d->%d %s->%s!\n", i, tail, pool_solvid2str(pool, od->tes[i].p), pool_solvid2str(pool, od->tes[tail].p));
720 j -= te->edges; /* in case we move */
721 addteedge(od, i, tail, TYPE_CYCLETAIL);
723 break; /* one edge is enough */
727 /* now add all head cycle edges */
730 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
733 for (i = 0; (j = cycle[i]) != 0; i++)
738 /* first the head to save some time */
740 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
742 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
744 if (!od->tes[k].mark)
746 if (od->tes[k].mark == -1)
747 od->tes[k].mark = -2; /* no need for another edge */
749 for (i = 0; cycle[i] != 0; i++)
751 if (cycle[i] == head)
753 te = od->tes + cycle[i];
754 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
756 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
758 /* see if we can reach a cycle TE from k */
759 if (!od->tes[k].mark)
761 if (od->tes[k].mark == -1)
764 printf("adding FROM HEAD cycle edge %d->%d %s->%s [%s]!\n", head, k, pool_solvid2str(pool, od->tes[head].p), pool_solvid2str(pool, od->tes[k].p), pool_solvid2str(pool, od->tes[cycle[i]].p));
766 addteedge(od, head, k, TYPE_CYCLEHEAD);
767 od->tes[k].mark = -2; /* no need to add that one again */
774 transaction_order(Transaction *trans, int flags)
776 Pool *pool = trans->pool;
777 Queue *tr = &trans->steps;
778 Repo *installed = pool->installed;
781 int i, j, k, numte, numedge;
783 struct s_TransactionElement *te;
784 Queue todo, obsq, samerepoq, uninstq;
793 start = now = solv_timems(0);
794 POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
795 /* free old data if present */
796 if (trans->orderdata)
798 struct s_TransactionOrderdata *od = trans->orderdata;
799 od->tes = solv_free(od->tes);
800 od->invedgedata = solv_free(od->invedgedata);
801 trans->orderdata = solv_free(trans->orderdata);
804 /* create a transaction element for every active component */
806 for (i = 0; i < tr->count; i++)
809 s = pool->solvables + p;
810 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
814 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
816 return; /* nothing to do... */
818 numte++; /* leave first one zero */
819 memset(&od, 0, sizeof(od));
822 od.tes = solv_calloc(numte, sizeof(*od.tes));
823 od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
826 queue_init(&od.cycles);
829 for (i = 0, te = od.tes + 1; i < tr->count; i++)
832 s = pool->solvables + p;
833 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
839 /* create dependency graph */
840 for (i = 0; i < tr->count; i++)
841 addsolvableedges(&od, pool->solvables + tr->elements[i]);
845 for (i = 1, te = od.tes + i; i < numte; i++, te++)
846 for (j = te->edges; od.edgedata[j]; j += 2)
848 POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
849 POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
855 now = solv_timems(0);
856 /* kill all cycles */
858 for (i = numte - 1; i > 0; i--)
859 queue_push(&todo, i);
863 i = queue_pop(&todo);
864 /* printf("- look at TE %d\n", i); */
868 od.tes[i].mark = 2; /* done with that one */
873 continue; /* already finished before */
876 int edgestovisit = 0;
877 /* new node, visit edges */
878 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
880 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
882 if (od.tes[k].mark == 2)
883 continue; /* no need to visit again */
885 queue_push(&todo, -i); /* end of edges marker */
886 queue_push(&todo, k);
889 te->mark = 2; /* no edges, done with that one */
891 te->mark = 1; /* under investigation */
894 /* oh no, we found a cycle */
895 /* find start of cycle node (<0) */
896 for (j = todo.count - 1; j >= 0; j--)
897 if (todo.elements[j] == -i)
901 /* build te/edge chain */
903 for (j = k; j < todo.count; j++)
904 if (todo.elements[j] < 0)
905 todo.elements[k++] = -todo.elements[j];
906 cycel = k - cycstart;
908 /* make room for edges, two extra element for cycle loop + terminating 0 */
909 while (todo.count < cycstart + 2 * cycel + 2)
910 queue_push(&todo, 0);
911 cycle = todo.elements + cycstart;
912 cycle[cycel] = i; /* close the loop */
913 cycle[2 * cycel + 1] = 0; /* terminator */
914 for (k = cycel; k > 0; k--)
916 cycle[k * 2] = cycle[k];
917 te = od.tes + cycle[k - 1];
918 assert(te->mark == 1);
919 te->mark = 0; /* reset investigation marker */
920 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
921 for (j = te->edges; od.edgedata[j]; j += 2)
922 if (od.edgedata[j] == cycle[k])
924 assert(od.edgedata[j]);
925 cycle[k * 2 - 1] = j;
927 /* now cycle looks like this: */
928 /* te1 edge te2 edge te3 ... teN edge te1 0 */
929 breakcycle(&od, cycle);
930 /* restart with start of cycle */
931 todo.count = cycstart + 1;
933 POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
934 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
936 now = solv_timems(0);
937 /* now go through all broken cycles and create cycle edges to help
939 for (i = od.cycles.count - 4; i >= 0; i -= 4)
941 if (od.cycles.elements[i + 2] >= TYPE_REQ)
942 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
944 for (i = od.cycles.count - 4; i >= 0; i -= 4)
946 if (od.cycles.elements[i + 2] < TYPE_REQ)
947 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
949 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
954 /* all edges are finally set up and there are no cycles, now the easy part.
955 * Create an ordered transaction */
956 now = solv_timems(0);
957 /* first invert all edges */
958 for (i = 1, te = od.tes + i; i < numte; i++, te++)
959 te->mark = 1; /* term 0 */
960 for (i = 1, te = od.tes + i; i < numte; i++, te++)
962 for (j = te->edges; od.edgedata[j]; j += 2)
964 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
966 od.tes[od.edgedata[j]].mark++;
970 for (i = 1, te = od.tes + i; i < numte; i++, te++)
975 POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
976 od.invedgedata = solv_calloc(j + 1, sizeof(Id));
977 for (i = 1, te = od.tes + i; i < numte; i++, te++)
979 for (j = te->edges; od.edgedata[j]; j += 2)
981 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
983 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
986 for (i = 1, te = od.tes + i; i < numte; i++, te++)
987 te->edges = te->mark; /* edges now points into invedgedata */
988 od.edgedata = solv_free(od.edgedata);
989 od.nedgedata = j + 1;
991 /* now the final ordering */
992 for (i = 1, te = od.tes + i; i < numte; i++, te++)
994 for (i = 1, te = od.tes + i; i < numte; i++, te++)
995 for (j = te->edges; od.invedgedata[j]; j++)
996 od.tes[od.invedgedata[j]].mark++;
998 queue_init(&samerepoq);
999 queue_init(&uninstq);
1001 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1004 if (installed && pool->solvables[te->p].repo == installed)
1005 queue_push(&uninstq, i);
1007 queue_push(&todo, i);
1009 assert(todo.count > 0 || uninstq.count > 0);
1010 oldcount = tr->count;
1017 temedianr = solv_calloc(numte, sizeof(Id));
1018 for (i = 1; i < numte; i++)
1020 Solvable *s = pool->solvables + od.tes[i].p;
1021 if (installed && s->repo == installed)
1024 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1029 /* select an TE i */
1031 i = queue_shift(&uninstq);
1032 else if (samerepoq.count)
1033 i = queue_shift(&samerepoq);
1034 else if (todo.count)
1036 /* find next repo/media */
1037 for (j = 0; j < todo.count; j++)
1039 if (!j || temedianr[todo.elements[j]] < lastmedia)
1042 lastmedia = temedianr[todo.elements[j]];
1045 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1047 /* move all matching TEs to samerepoq */
1048 for (i = j = 0; j < todo.count; j++)
1050 int k = todo.elements[j];
1051 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1052 queue_push(&samerepoq, k);
1054 todo.elements[i++] = k;
1058 assert(samerepoq.count);
1059 i = queue_shift(&samerepoq);
1065 queue_push(tr, te->p);
1067 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1069 for (j = te->edges; od.invedgedata[j]; j++)
1071 struct s_TransactionElement *te2 = od.tes + od.invedgedata[j];
1072 assert(te2->mark > 0);
1073 if (--te2->mark == 0)
1075 Solvable *s = pool->solvables + te2->p;
1077 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1079 if (installed && s->repo == installed)
1080 queue_push(&uninstq, od.invedgedata[j]);
1081 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1082 queue_push(&samerepoq, od.invedgedata[j]);
1084 queue_push(&todo, od.invedgedata[j]);
1088 solv_free(temedianr);
1090 queue_free(&samerepoq);
1091 queue_free(&uninstq);
1093 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1094 assert(te->mark == 0);
1096 /* add back obsoleted packages */
1097 transaction_add_obsoleted(trans);
1098 assert(tr->count == oldcount);
1100 POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1101 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1103 if ((flags & (SOLVER_TRANSACTION_KEEP_ORDERDATA | SOLVER_TRANSACTION_KEEP_ORDERCYCLES)) != 0)
1105 struct s_TransactionOrderdata *tod;
1106 trans->orderdata = tod = solv_calloc(1, sizeof(*trans->orderdata));
1107 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERCYCLES) != 0)
1109 Queue *cycles = tod->cycles = solv_calloc(1, sizeof(Queue));
1110 queue_init_clone(cycles, &od.cyclesdata);
1111 /* map from tes to packages */
1112 for (i = 0; i < cycles->count; i++)
1113 if (cycles->elements[i])
1114 cycles->elements[i] = od.tes[cycles->elements[i]].p;
1115 queue_insertn(cycles, cycles->count, od.cycles.count, od.cycles.elements);
1116 queue_push(cycles, od.cycles.count / 4);
1118 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1122 tod->invedgedata = od.invedgedata;
1123 tod->ninvedgedata = od.nedgedata;
1129 solv_free(od.invedgedata);
1130 queue_free(&od.cycles);
1131 queue_free(&od.cyclesdata);
1136 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1139 struct s_TransactionOrderdata *od = trans->orderdata;
1140 struct s_TransactionElement *te;
1143 return choices->count;
1146 /* initialization step */
1147 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1149 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1151 for (j = te->edges; od->invedgedata[j]; j++)
1152 od->tes[od->invedgedata[j]].mark++;
1154 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1156 queue_push(choices, te->p);
1157 return choices->count;
1159 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1160 if (te->p == chosen)
1163 return choices->count;
1166 /* hey! out-of-order installation! */
1169 for (j = te->edges; od->invedgedata[j]; j++)
1171 te = od->tes + od->invedgedata[j];
1172 assert(te->mark > 0 || te->mark == -1);
1173 if (te->mark > 0 && --te->mark == 0)
1174 queue_push(choices, te->p);
1176 return choices->count;
1180 transaction_add_obsoleted(Transaction *trans)
1182 Pool *pool = trans->pool;
1183 Repo *installed = pool->installed;
1190 if (!installed || !trans->steps.count)
1192 /* calculate upper bound */
1194 FOR_REPO_SOLVABLES(installed, p, s)
1195 if (MAPTST(&trans->transactsmap, p))
1200 steps = &trans->steps;
1201 queue_insertn(steps, 0, max, 0);
1204 map_init(&done, installed->end - installed->start);
1206 for (j = 0, i = max; i < steps->count; i++)
1208 p = trans->steps.elements[i];
1209 if (pool->solvables[p].repo == installed)
1211 if (!trans->transaction_installed[p - pool->installed->start])
1212 trans->steps.elements[j++] = p;
1215 trans->steps.elements[j++] = p;
1217 transaction_all_obs_pkgs(trans, p, &obsq);
1218 for (k = 0; k < obsq.count; k++)
1220 p = obsq.elements[k];
1221 assert(p >= installed->start && p < installed->end);
1222 if (!MAPTST(&trans->transactsmap, p)) /* just in case */
1224 if (MAPTST(&done, p - installed->start))
1226 MAPSET(&done, p - installed->start);
1227 trans->steps.elements[j++] = p;
1231 /* free unneeded space */
1232 queue_truncate(steps, j);
1238 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1240 Pool *pool = trans->pool;
1245 if (MAPTST(seen, pkg))
1248 s = pool->solvables + pkg;
1250 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1256 reqp = s->repo->idarraydata + s->requires;
1257 while ((req = *reqp++) != 0)
1259 if (req == SOLVABLE_PREREQMARKER)
1264 if (onlyprereq && !inpre)
1266 if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
1269 /* first check kept packages, then freshly installed, then not yet uninstalled */
1270 FOR_PROVIDES(p, pp, req)
1272 if (!MAPTST(ins, p))
1274 if (MAPTST(&trans->transactsmap, p))
1277 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1281 FOR_PROVIDES(p, pp, req)
1283 if (!MAPTST(ins, p))
1285 if (pool->solvables[p].repo == pool->installed)
1288 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1293 FOR_PROVIDES(p, pp, req)
1295 if (!MAPTST(ins, p))
1298 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1303 POOL_DEBUG(SOLV_DEBUG_RESULT, " %c%s: nothing provides %s needed by %c%s\n", pool->solvables[tepkg].repo == pool->installed ? '-' : '+', pool_solvid2str(pool, tepkg), pool_dep2str(pool, req), s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1310 transaction_check_order(Transaction *trans)
1312 Pool *pool = trans->pool;
1318 POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
1319 map_init(&ins, pool->nsolvables);
1320 map_init(&seen, pool->nsolvables);
1321 if (pool->installed)
1323 FOR_REPO_SOLVABLES(pool->installed, p, s)
1327 for (i = 0; i < trans->steps.count; i++)
1329 p = trans->steps.elements[i];
1330 s = pool->solvables + p;
1331 if (s->repo != pool->installed)
1333 if (s->repo != pool->installed)
1335 if (havescripts(pool, p))
1338 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1340 if (s->repo == pool->installed)
1345 POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");
1349 transaction_order_get_cycleids(Transaction *trans, Queue *q, int minseverity)
1351 struct s_TransactionOrderdata *od = trans->orderdata;
1353 int i, cid, ncycles;
1356 if (!od || !od->cycles || !od->cycles->count)
1359 ncycles = cq->elements[cq->count - 1];
1360 i = cq->count - 1 - ncycles * 4;
1361 for (cid = 1; cid <= ncycles; cid++, i += 4)
1365 int cmin = cq->elements[i + 3] & 0xffff;
1366 int cmax = (cq->elements[i + 3] >> 16) & 0xffff;
1367 if (minseverity >= SOLVER_ORDERCYCLE_NORMAL && cmin < TYPE_REQ)
1369 if (minseverity >= SOLVER_ORDERCYCLE_CRITICAL && (cmax & TYPE_PREREQ) == 0)
1377 transaction_order_get_cycle(Transaction *trans, Id cid, Queue *q)
1379 struct s_TransactionOrderdata *od = trans->orderdata;
1381 int cmin, cmax, severity;
1385 if (!od || !od->cycles || !od->cycles->count)
1386 return SOLVER_ORDERCYCLE_HARMLESS;
1388 ncycles = cq->elements[cq->count - 1];
1389 if (cid < 1 || cid > ncycles)
1390 return SOLVER_ORDERCYCLE_HARMLESS;
1391 cid = cq->count - 1 - 4 * (ncycles - cid + 1);
1392 cmin = cq->elements[cid + 3] & 0xffff;
1393 cmax = (cq->elements[cid + 3] >> 16) & 0xffff;
1394 if (cmin < TYPE_REQ)
1395 severity = SOLVER_ORDERCYCLE_HARMLESS;
1396 else if ((cmax & TYPE_PREREQ) == 0)
1397 severity = SOLVER_ORDERCYCLE_NORMAL;
1399 severity = SOLVER_ORDERCYCLE_CRITICAL;
1401 queue_insertn(q, 0, cq->elements[cid + 1], cq->elements + cq->elements[cid]);